Kaynağa Gözat

Merge pull request #3195 from weseek/imprve/4614-4633-refactor-aws-for-issue-signed-url

Imprve/4614 4633 refactor aws for issue signed url
Yuki Takei 5 yıl önce
ebeveyn
işleme
4c39586369

+ 25 - 0
src/server/service/file-uploader/aws.js

@@ -72,6 +72,31 @@ module.exports = function(crowi) {
       && this.configManager.getConfig('crowi', 'aws:s3Bucket') != null;
   };
 
+  lib.canRespond = function() {
+    // TODO retrieve bool by getConfig
+    return true;
+  };
+
+  lib.respond = async function(res, attachment) {
+    if (!this.getIsUploadable()) {
+      throw new Error('AWS is not configured.');
+    }
+
+    const s3 = S3Factory();
+    const awsConfig = getAwsConfig();
+    const filePath = getFilePathOnStorage(attachment);
+
+    // issue signed url for 30 seconds
+    // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property
+    const params = {
+      Bucket: awsConfig.bucket,
+      Key: filePath,
+      Expires: 30,
+    };
+    const signedUrl = s3.getSignedUrl('getObject', params);
+
+    return res.redirect(signedUrl);
+  };
 
   lib.deleteFile = async function(attachment) {
     const filePath = getFilePathOnStorage(attachment);

+ 5 - 2
src/server/service/file-uploader/gcs.js

@@ -56,8 +56,11 @@ module.exports = function(crowi) {
   };
 
   lib.respond = async function(res, attachment) {
-    // TODO refacotr this code after GW-4630
-    const gcs = getGcsInstance(this.getIsUploadable());
+    if (!this.getIsUploadable()) {
+      throw new Error('GCS is not configured.');
+    }
+
+    const gcs = getGcsInstance();
     const myBucket = gcs.bucket(getGcsBucket());
     const filePath = getFilePathOnStorage(attachment);
     const file = myBucket.file(filePath);