Google 存储上传文件 javascript
Google storage upload files in javascript
我正在寻找将文件上传到存储桶的 absolute minimal
要求。以及所需的最少代码。
我拥有的最少 oAuth/API-key 信息来自 console.cloud.google.com/apis/credentials/oauthclient/
clientId
:来自凭据页面的客户端 ID。
apiKey
:来自凭据页面的客户端机密?
scopes
: 'https://www.googleapis.com/auth/devstorage.read_write'(为什么叫devstorage
?好像是专门为开发者准备的?还是device
的缩写?
我不确定 apiKey 是否正确,但是在示例中使用它不会引发错误:
// This is triggered after the `client.js` loads
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
所以,这是第一步,如果我错了请纠正我。
现在处理验证结果:
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
makeApiCall();
}
}
这里我有点卡住了,有一个我正在研究的例子:https://developers.google.com/api-client-library/javascript/start/start-js
到达 gapi.client.plus.people
时停止工作,其中 people
未定义。这是因为我有一个匿名用户,有点像 public API-key。稍后我将实现每个用户的 ACL,但现在我只需要它 work/upload.
现在,我在亚马逊云上有类似的工作,有一个简单的 jQuery-based 小部件可以上传文件,我只需要输入他们的 API-key
和基本的存储桶名称(不幸的是 PHP
。
只要一个简单的<form>
我就很高兴了,但是我遇到的例子比上面的3个最小数据(clientId,apiKey更多information/fields , 范围) 和额外的 url 到存储桶 (像奇怪的加密 acl 字符串)
我知道 bucket-name.storage.googleapis.com
是文件的最终位置,当我手动上传图片时,它会起作用。
我现在正在寻找绝对最小的代码,最好是 javascript,使用 google client
所以使用类似的东西(我在另一个例子中找到):
gapi.client.request({
'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o',
'method': 'POST',
'params': {'uploadType': 'media'}
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
}
我需要API_VERSION
吗?我如何找出要使用的 path
?我知道我的 bucket
名字,但是 upload/storage/
是从哪里来的? /b/
? (虽然 https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload 表明我需要它)
该示例使用 header 和 boundary
但我找不到任何相关信息,而且我可能也不需要它。问题是我找不到任何 minimally
所需的 header。
我遇到了 'x-goog-acl': 'public-read'
,这是否需要在 header 中?
综上所述,我想回答的问题:
- 我至少需要将什么 header 上传到
allUsers
访问权限设置为 Owner
的存储桶(我知道,这不是最好的主意,这在未来会改变)
- 如何从
html
表单获取文件的任何示例代码,以及使用 ajax
发送请求,或使用 gapi
,我认为这意味着这样做。
我希望我的问题很清楚,我基本上不知道从哪里 start/find 正确的代码示例。尽管这可能与 google javascript 客户端 api 在 beta
..
中的事实有关
What headers would I minimally need to upload to a bucket with allUsers access set to Owner ( I know, not the best idea, this will change in the future).
你说得对!这不是一个好主意。如果匿名用户拥有您的存储桶,这意味着他们可以删除其中的任何对象,覆盖现有对象,否则会造成代价高昂的恶作剧。尽管如此,你的问题的答案是:
<form action="https://storage.googleapis.com/YOUR_BUCKET_NAME"
method="post" enctype="multipart/form-data">
<input name="key" type="text" value="objectName.txt" /><br/>
<input name="file" type="file" /><br/>
<input type="submit" value="Upload!" />
</form>
这是上传对象所需的绝对最低要求。请注意,它不会将您重定向到任何地方或以其他方式指示成功。为此,您需要使用 success_action_redirect 参数,并且要求绝对最小值。
我正在寻找将文件上传到存储桶的 absolute minimal
要求。以及所需的最少代码。
我拥有的最少 oAuth/API-key 信息来自 console.cloud.google.com/apis/credentials/oauthclient/
clientId
:来自凭据页面的客户端 ID。apiKey
:来自凭据页面的客户端机密?scopes
: 'https://www.googleapis.com/auth/devstorage.read_write'(为什么叫devstorage
?好像是专门为开发者准备的?还是device
的缩写?
我不确定 apiKey 是否正确,但是在示例中使用它不会引发错误:
// This is triggered after the `client.js` loads
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
所以,这是第一步,如果我错了请纠正我。
现在处理验证结果:
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
makeApiCall();
}
}
这里我有点卡住了,有一个我正在研究的例子:https://developers.google.com/api-client-library/javascript/start/start-js
到达 gapi.client.plus.people
时停止工作,其中 people
未定义。这是因为我有一个匿名用户,有点像 public API-key。稍后我将实现每个用户的 ACL,但现在我只需要它 work/upload.
现在,我在亚马逊云上有类似的工作,有一个简单的 jQuery-based 小部件可以上传文件,我只需要输入他们的 API-key
和基本的存储桶名称(不幸的是 PHP
。
只要一个简单的<form>
我就很高兴了,但是我遇到的例子比上面的3个最小数据(clientId,apiKey更多information/fields , 范围) 和额外的 url 到存储桶 (像奇怪的加密 acl 字符串)
我知道 bucket-name.storage.googleapis.com
是文件的最终位置,当我手动上传图片时,它会起作用。
我现在正在寻找绝对最小的代码,最好是 javascript,使用 google client
所以使用类似的东西(我在另一个例子中找到):
gapi.client.request({
'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o',
'method': 'POST',
'params': {'uploadType': 'media'}
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
}
我需要API_VERSION
吗?我如何找出要使用的 path
?我知道我的 bucket
名字,但是 upload/storage/
是从哪里来的? /b/
? (虽然 https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload 表明我需要它)
该示例使用 header 和 boundary
但我找不到任何相关信息,而且我可能也不需要它。问题是我找不到任何 minimally
所需的 header。
我遇到了 'x-goog-acl': 'public-read'
,这是否需要在 header 中?
综上所述,我想回答的问题:
- 我至少需要将什么 header 上传到
allUsers
访问权限设置为Owner
的存储桶(我知道,这不是最好的主意,这在未来会改变) - 如何从
html
表单获取文件的任何示例代码,以及使用ajax
发送请求,或使用gapi
,我认为这意味着这样做。
我希望我的问题很清楚,我基本上不知道从哪里 start/find 正确的代码示例。尽管这可能与 google javascript 客户端 api 在 beta
..
What headers would I minimally need to upload to a bucket with allUsers access set to Owner ( I know, not the best idea, this will change in the future).
你说得对!这不是一个好主意。如果匿名用户拥有您的存储桶,这意味着他们可以删除其中的任何对象,覆盖现有对象,否则会造成代价高昂的恶作剧。尽管如此,你的问题的答案是:
<form action="https://storage.googleapis.com/YOUR_BUCKET_NAME"
method="post" enctype="multipart/form-data">
<input name="key" type="text" value="objectName.txt" /><br/>
<input name="file" type="file" /><br/>
<input type="submit" value="Upload!" />
</form>
这是上传对象所需的绝对最低要求。请注意,它不会将您重定向到任何地方或以其他方式指示成功。为此,您需要使用 success_action_redirect 参数,并且要求绝对最小值。