使用 @aws-sdk/lib-storage 将文件上传到 AWS S3 时访问位置

Jam*_*unt 5 javascript amazon-s3 aws-sdk-js

我目前正在将项目从 SDK v2 升级到 V3,并且很难访问要上传到 S3 存储桶的文件的位置 URL。

目前,以下实现无法访问,location因为它不存在于AbortMultipartUploadCommandOutput响应类型中。

try {
    const x = new Upload({
      client: new S3Client({ region: "us-east-2" }),
      params: {
        Bucket: BUCKET_NAME,
        Key: filename.toString(),
        Body: buffer
      }
    })


    const response = await x.done()
    
    const locationUrl = response.location

    return locationUrl

  } catch (error) {
    console.log(error)
  }
Run Code Online (Sandbox Code Playgroud)

我一直在探索的另一个解决方案是使用PutObjectCommand.

const command = new PutObjectCommand({
    Bucket: BUCKET_NAME,
    Key: `${path}/${fileName}`,
    Body: fileContent,
    ContentType: "text/csv"
  })
Run Code Online (Sandbox Code Playgroud)

文件上传到 S3 后,有哪些可能的解决方案可以访问我的文件信息?

小智 0

如果响应200,您可以自己创建

location = https://${BUCKET_NAME}.s3.${AWS_REGION}.amazonaws.com/${Key}
Run Code Online (Sandbox Code Playgroud)