2

我有下面我localstack在我的机器上使用的代码。当我列出存储桶时它会起作用。但是当我做listObjects时它返回空数组。

import * as AWS from "aws-sdk";

let s3 = new AWS.S3({endpoint: 'http://localhost:4572', region: 'us-east-1'});

(async () => {

  // list buckets works, returns all my local buckets
  let buckets = await s3.listBuckets().promise()
  console.log('buckets: ', buckets)

  // this returns empty array
  let listOfobjects = await s3.listObjects({Bucket: 'store-us-east-1'}).promise()
  console.log('listOfobjects: ', listOfobjects);
})()


# ls works via command line, return Invoice.pdf
aws --endpoint-url=http://localhost:4572 s3 ls s3://store-us-east-1
2020-01-15 18:02:55      82032 Invoice.pdf

任何帮助将不胜感激。

4

1 回答 1

0

对,listObjects在 localstack 中由于存储桶地址而 ${BUCKET_NAME}.${IP_ADDRESS}不是预期格式而失败。您只需将其添加到您的 S3 实例化中:

let s3 = new AWS.S3({
    endpoint: 'http://localhost:4572', 
    region: 'us-east-1',
    sslEnabled: false,
    s3ForcePathStyle: true 
});

此外,并不是说它很重要,但我建议对端点使用类似的东西:

endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:4572`
于 2021-04-30T14:23:57.980 回答