将图像上载到S3并检索图像的唯一URL

时间:2023-01-29 23:04:01

I am uploading 3 different images to S3, using this code:

我使用以下代码将3个不同的图像上传到S3:

AWS.config.update({
    accessKeyId: accessKeyId,
    secretAccessKey: secretAccessKey
});

const s3Stream = require('s3-upload-stream')(new AWS.S3())

function getUploadStrm() {

    return s3Stream.upload({
        Bucket: awsS3Bucket,
        Key: accessKeyId,
        ACL: 'public-read',
        StorageClass: 'REDUCED_REDUNDANCY',
        ContentType: 'binary/octet-stream',
    });
}

(I created 3 upload streams and write to each stream separately).

(我创建了3个上传流并分别写入每个流)。

and this is the information I get back:

这是我得到的信息:

detail =>  { Location: 'https://staging-media-assets.s3.amazonaws.com/AKIAIRNYI65C5VKWDEDQ',
  Bucket: 'staging-media-assets',
  Key: 'AKIAIRNYI65XC5VKWDEDQ',
  ETag: '"d7165b042c4068477d03341ca75x518e1-1"' }

detail =>  { Location: 'https://staging-media-assets.s3.amazonaws.com/AKIAIRNYI65C5VKWDEDQ',
  Bucket: 'staging-media-assets',
  Key: 'AKIAIRNYI65XC5VKWDEDQ',
  ETag: '"e325f4cb98e4af408e60e421e37xa4a27-1"' }

detail =>  { Location: 'https://staging-media-assets.s3.amazonaws.com/AKIAIRNYI65C5VKWDEDQ',
  Bucket: 'staging-media-assets',
  Key: 'AKIAIRNYI65XC5VKWDEDQ',
  ETag: '"8a04422756e20eb9f93718422fx11dad8-1"' }

My question is - how can I locate the URL of the images that were uploaded given the information that was returned?

我的问题是 - 如何根据返回的信息找到上传的图像的URL?

The information on the S3 site does not make it clear to me:

S3网站上的信息对我来说并不清楚:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createMultipartUpload-property

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#createMultipartUpload-property

1 个解决方案

#1


1  

Given those responses, you appear to be using the same Key value every time, so you are overwriting the file on S3 each time. It looks like you are passing your AWS Access key as the S3 key, which is incorrect. The S3 key is the unique ID for the object you are uploading. You need to specify unique values for key in the parameters you are passing to s3Stream.upload(). The Location value you get back will be the URL for the file on S3.

鉴于这些响应,您似乎每次都使用相同的Key值,因此您每次都会在S3上覆盖该文件。您似乎将AWS Access密钥作为S3密钥传递,这是不正确的。 S3键是要上载的对象的唯一ID。您需要在传递给s3Stream.upload()的参数中为key指定唯一值。您获得的位置值将是S3上文件的URL。

#1


1  

Given those responses, you appear to be using the same Key value every time, so you are overwriting the file on S3 each time. It looks like you are passing your AWS Access key as the S3 key, which is incorrect. The S3 key is the unique ID for the object you are uploading. You need to specify unique values for key in the parameters you are passing to s3Stream.upload(). The Location value you get back will be the URL for the file on S3.

鉴于这些响应,您似乎每次都使用相同的Key值,因此您每次都会在S3上覆盖该文件。您似乎将AWS Access密钥作为S3密钥传递,这是不正确的。 S3键是要上载的对象的唯一ID。您需要在传递给s3Stream.upload()的参数中为key指定唯一值。您获得的位置值将是S3上文件的URL。