如何使用Firebase云功能为DigitalOCean Space创建预签名的url?

时间:2019-11-14 08:03:07

标签: android node.js aws-sdk digital-ocean pre-signed-url

我正在创建一个Android应用,用户可以在其中观看视频。我正在使用DigitalOcean托管视频,并使用Firebase实时数据库作为数据库。当用户单击观看视频时,我正在使用Firebase可调用云功能为视频生成预签名网址。我听说AWS SDK与DigitalOCean兼容,因此我正在使用AWS SDK for Firebase函数生成预签名的URL。但是我无法获得确切的URL。我得到如下所示的URL-

https://myappvideo.sgp1.digitaloceanspaces.com/video14.mp4?AWSAccessKeyId=myAccessKeyId&Content-Type=video%2Fmp4&Expires=1573718047&Signature=Z07GtSybHwbc7m4%2FSczqeYQ%2FwpI%3D&x-amz-acl=public-read&x-amz-server-side-encryption=AES256

此处“ myAccessKeyId”是我的DigitalOcean空间的访问密钥ID。为了安全起见,我在此问题中将其删除。

当我将网址粘贴到chrome中时,它显示以下错误-

<Error>
 <Code>SignatureDoesNotMatch</Code>
 <RequestId>tx0000000000000bb11173b-005dcd05da-501552-sgp1a</RequestId>
 <HostId>501552-sgp1a-sgp</HostId>
</Error>

我正在使用的Firebase函数如下-

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const AWS = require('aws-sdk');

exports.getS3SignedUrlUpload = functions.https.onCall((data, context) => {

   const spacesEndpoint = new AWS.Endpoint('https://sgp1.digitaloceanspaces.com');

   AWS.config.update({
     endpoint: spacesEndpoint,
     accessKeyId: "MyAccessKeyId",
     secretAccessKey: "MySecretAccessKey",
     signatureVersion: 'v4',
     region: "sgp1" 
   });

   var s3 = new AWS.S3();

   const s3Params = {

     Bucket: "myappvideo", // My DigitalOcean Bucket name
     Key: "video14.mp4",  //The video for which I want to generate pre-signed url
     Expires: 600, // Expires in 10 minutes
     ContentType: "video/mp4",
     ACL: 'public-read',// What is this I don't know, please tell me if you know
     ServerSideEncryption: 'AES256'  // What is this I don't know, please tell me if you know

   };

  return s3.getSignedUrl('putObject', s3Params);

});

我不太了解javaScript和编写云函数。请告诉我在哪里进行更改以获取正确的视频预签名URL。

0 个答案:

没有答案