Amazon S3浏览器直接上传唯一文件名

时间:2022-11-19 09:27:46

I'm using Node.js and the Amazon aws sdk for signing an upload request so I can do direct uploads from an Angular application. It works really well except I can't get unique file names. I have tried with creating unique buckets instead but then I can't get the CORS settings to work. Since it's not possible to rename the file in the browser, this is all a mystery to me. Does anyone have experience with this?

我正在使用Node.js和Amazon aws sdk来签署上传请求,因此我可以从Angular应用程序直接上传。它工作得非常好,除了我无法获得唯一的文件名。我尝试过创建独特的存储桶,但后来我无法使用CORS设置。由于无法在浏览器中重命名文件,这对我来说都是一个谜。有人对此有经验吗?

1 个解决方案

#1


1  

The resulting LoopBack.io remote method now looks like below, setting 'Key' in the parameters did the trick.

生成的LoopBack.io远程方法现在如下所示,在参数中设置'Key'就可以了。

Project.signS3 = function(filename, cb){
    var aws = require('aws-sdk');

    var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
    var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
    var S3_BUCKET = '...';

    aws.config.update({
        accessKeyId: AWS_ACCESS_KEY, 
        secretAccessKey: AWS_SECRET_KEY, 
        region: 'eu-central-1',
        signatureVersion: 'v4'
    });

    // Figure out a unique filename
    var ext = filename.split('.').pop();
    var random = Math.floor(Math.random() * 900000000000000000);

    filename = random + '.' + ext;

    var s3 = new aws.S3();
    var s3_params = {
        Bucket: S3_BUCKET,
        Key: filename,
        Expires: 60,
        ACL: 'public-read'
    };

    s3.getSignedUrl('putObject', s3_params, function(err, data){

        if(err){
            console.log(err);
        }
        else{
            var return_data = {
                signed_request: data,
                url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+filename
            };

            cb(null, return_data);
        }
    });
}

#1


1  

The resulting LoopBack.io remote method now looks like below, setting 'Key' in the parameters did the trick.

生成的LoopBack.io远程方法现在如下所示,在参数中设置'Key'就可以了。

Project.signS3 = function(filename, cb){
    var aws = require('aws-sdk');

    var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
    var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY;
    var S3_BUCKET = '...';

    aws.config.update({
        accessKeyId: AWS_ACCESS_KEY, 
        secretAccessKey: AWS_SECRET_KEY, 
        region: 'eu-central-1',
        signatureVersion: 'v4'
    });

    // Figure out a unique filename
    var ext = filename.split('.').pop();
    var random = Math.floor(Math.random() * 900000000000000000);

    filename = random + '.' + ext;

    var s3 = new aws.S3();
    var s3_params = {
        Bucket: S3_BUCKET,
        Key: filename,
        Expires: 60,
        ACL: 'public-read'
    };

    s3.getSignedUrl('putObject', s3_params, function(err, data){

        if(err){
            console.log(err);
        }
        else{
            var return_data = {
                signed_request: data,
                url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+filename
            };

            cb(null, return_data);
        }
    });
}