简体   繁体   English

使用 javascript 上传到谷歌云存储签名 url

[英]upload to google cloud storage signed url with javascript

With the following code I'm able to upload to my publicly writable bucket in google cloud storage.使用以下代码,我可以上传到我在谷歌云存储中的公开可写存储桶。 (allUsers has write permission). (allUsers 有写权限)。 However If the bucket isn't publicly writable then I get a 401 unauthorised error.但是,如果存储桶不可公开写入,那么我会收到 401 未经授权的错误。 (I don't want the bucket to be publicly writable). (我不希望存储桶可公开写入)。

var file = $scope.myFile;
      var fileData = file;
      var boundary = '-------314159265358979323846';
      var delimiter = "\r\n--" + boundary + "\r\n";
      var close_delim = "\r\n--" + boundary + "--";

      var reader = new FileReader();
      reader.readAsBinaryString(fileData);
      reader.onload = function(e) {
        var contentType = fileData.type || 'application/octet-stream';
        var metadata = {
          'name': 'objectName', //'lol' + fileData.name,
          'mimeType': contentType
        };

        var base64Data = btoa(reader.result);
        var multipartRequestBody =
          delimiter +
          'Content-Type: application/json\r\n\r\n' +
          JSON.stringify(metadata) +
          delimiter +
          'Content-Type: ' + contentType + '\r\n' +
          'Content-Transfer-Encoding: base64\r\n' +
          '\r\n' +
          base64Data +
          close_delim;
        var stuff = angular.fromJson('{"Expires": "1415344534", "GoogleAccessId": "394062384276-n2jjh17vt975fsi4nc9ikm1nj55466ir@developer.gserviceaccount.com", "Signature": "AMkhO7mt2zg+s1Dzx28yQIMSrZlDC2Xx1SzvMCAgUVyiLXs5890/nA6PKzoc1KYBcRv/ALmkNaEVhvWHxE0EfcE151c0PYSG9x7AeSpQI/3dB1UPcSqpwilS1e2sgwB9piLNvBEXLNRXiLYyTiH22zkFZHAEQonJ3J25a47fwo4="}');
        var Expires = stuff.Expires;
        var GoogleAccessId = stuff.GoogleAccessId;
        var Signature = encodeURIComponent(stuff.Signature);
        var BUCKET = 'mybucket';
        var request = $window.gapi.client.request({
          'path': '/upload/storage/v1/b/' + BUCKET + '/o',
          'method': 'POST',
          'params': {
            'uploadType': 'multipart',
            'Expires': Expires,
            'GoogleAccessId': GoogleAccessId,
            'Signature': Signature
          },
          'headers': {
            'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
          },
          'body': multipartRequestBody});

        request.execute(function(r) {
          console.log(r);
        })
      }

Is it possible to use signed URLS with the gapi javascript client?是否可以在 gapi javascript 客户端中使用签名 URL? Or does it not understand the params.或者它不理解参数。

If not - are there any examples of doing CORS with the JSON api from javascript for upload with signed urls?如果没有 - 是否有任何示例使用来自 javascript 的 JSON api 执行 CORS 以使用签名的 url 上传?

(lets assume that my expiry, GoogleAccessId & Signature are correct & match what i'm doing in the javascript & the permissions i've set up on the bucket) (假设我的到期时间、GoogleAccessId 和签名是正确的并与我在 javascript 中所做的事情以及我在存储桶上设置的权限相匹配)

basically are there any examples of uploading to google cloud storage from javascript client from localhost without requiring the user to have a google account & without using a publicly writable bucket but using dispensed signed urls?基本上是否有任何示例可以从本地主机的 javascript 客户端上传到谷歌云存储,而无需用户拥有谷歌帐户,并且无需使用可公开写入的存储桶,但使用分配的签名网址?

Use https://storage.googleapis.com as a host to compose the URL that points to the desired resource.使用https://storage.googleapis.com作为主机来编写指向所需资源的 URL。 You can choose between a few ways to construct your base URL.您可以在几种构建基本 URL 的方法中进行选择。 Here are some possible combinations. 以下是一些可能的组合。

For reference, you can also check out a very simple snippet Python that could be helpful.作为参考,您还可以查看一个非常简单的 Python 片段,它可能会有所帮助。

Hope it helps.希望能帮助到你。

I was implementing the same issue.我正在实施同样的问题。 The problem is with SignedURL.问题出在 SignedURL 上。 After correcting the signedurl the upload worked like a charm.更正signedurl后,上传工作就像一个魅力。

As I was using php.因为我使用的是 php。 Below is the code for generating signed urls.下面是生成签名网址的代码。

private function createSignedUrl($objectName, $bucketName, $key, $serviceEmailAddress, $method = 'GET', $duration = 600)
{
    $expires = time() + $duration;

// Line breaks are important!
$toSign = (
    $method . "\n" .
    /* Content-MD5 */ "\n" .
    /* Content Type */ "\n" .
    $expires . "\n" .
    $objectName
);
$signature = urlencode(base64_encode(JWT::encode($toSign, $key, 'HS256')));
return array(
    'expires' => $expires,
    'accessid' => $serviceEmailAddress,
    'signature' => $signature,
);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Storage 签名 URL 上传 + Dropzone.js - Google Cloud Storage Signed URL Upload + Dropzone.js 无法使用已签名的 url 成功上传到 Google Cloud - Unable to successfully upload to Google Cloud with a signed url 使用节点js创建预签名网址并将文件上传到Google云端存储 - create a pre signed url and upload a file to google cloud storage using node js 通过javascript将文件上传到Google云存储 - Upload file to google cloud storage by javascript 如何在angular中使用谷歌云存储的签名URL下载文件 - How to download a file using the signed URL of google cloud storage in angular 如何从 Node 中的图像 url 将图像上传到 Google Cloud Storage? - How to upload an image to Google Cloud Storage from an image url in Node? 使用JavaScript从客户端浏览器上传到Google云端存储 - Upload from Client Browser to Google Cloud Storage Using JavaScript 使用javascript将请求批量上传到Google云端存储 - Batch upload requests to Google Cloud Storage using javascript Google Cloud Storage - 如何直接从浏览器上传内容 (JavaScript) - Google Cloud Storage - How to Upload content directly from browser (JavaScript) 使用node.js为Google云端存储创建已签名的网址,以便从浏览器直接上传 - Create signed URLs for Google Cloud Storage with node.js for direct upload from browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM