![](/img/trans.png)
[英]How to point AWS-SDK DynamoDB to a serverless DynamoDB local
[英]serverless-s3-local plugin and @aws-sdk/client-s3 returns an error with PutObjectCommand
我正在嘗試做的是使用假 s3(使用 serverless-s3-local 插件)保存文件 csv,但由於某種原因,端點出現錯誤。 我遵循了這些說明serverless-s3-local但使用了庫“@aws-sdk/client-s3”。
注意:我運行文檔aws --endpoint http://localhost:4569 s3 cp ~/tmp/data.csv s3://local-bucket/userdata.csv --profile s3local
的命令,一切正常。 所以也許問題出在我正在使用的參數或方法中,但我找不到解決方案。
這是我收到的以下錯誤:
Error: getaddrinfo ENOTFOUND local-bucket.localhost
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -3008, code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'local-bucket.localhost', '$metadata': { attempts: 1, totalRetryDelay: 0 } }
這些是我在 serverless.yml 的自定義和資源部分中的一些配置
custom:
# S3
s3:
bucketName: local-bucket
host: localhost
directory: /tmp
resources:
Resources:
NewResource:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.s3.bucketName}
這是我的時間代碼:
main.ts
import { S3Manager } from "../../../../shared/utils/aws/s3Manager";
private _contentTypeXlsx: string = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
private _contentTypeCsv: string = 'text/csv;charset=utf-8';
const s3Config= new S3Manager('S3RVER', 'S3RVER', 'us-east-1', 'local-bucket');
let bucketKey
let responseUpload
const fileName = `ExportedFile.csv`;
bucketKey = `ExportedFiles/${Date.now()}/${fileName}`;
const fileCsv = await new ObjectsToCsv(arrivals);
responseUpload = await s3Manager.uploadFiles({ Key: bucketKey, Body: fileCsv.toString(), ContentType: this._contentTypeCsv });
s3Config.ts
import { S3Client, PutObjectCommand, GetObjectCommand } from "@aws-sdk/client-s3";
export class S3Manager {
s3Client: any;
awsS3: any
bucketParams: any;
constructor(keyId: string, accessKey: string, region: string, bucketName: string) {
const configAwsClient = {
s3ForcePathStyle: true,
accessKeyId: keyId,
secretAccessKey: accessKey,
endpoint: 'http://localhost:4569',
};
this.s3Client = new S3Client(configAwsClient);
this.bucketParams = {
Bucket: bucketName
}
}
async uploadFiles(filters: {}): Promise<any> {
let success = true;
try {
var params = {
...this.bucketParams,
...filters
};
const command = await new PutObjectCommand(params);
const response = await this.s3Client.send(command);
console.info('Response of putObject', response);
}
catch (err) {
success = false;
console.log(err);
}
return success;
} }
我找到了解決這個問題的方法。 由於 aws-sdk 的 v3,需要更改屬性 s3ForcePathStyle 而不是 forcePathStyle。
const configAwsClient = {
forcePathStyle: true,
credentials: {
accessKeyId: keyId,
secretAccessKey: accessKey,
},
endpoint: 'http://localhost:4569',
};
如果您想查看有關此問題的更多詳細信息,解決方案已在此處解決: https://github.com/ar90n/serverless-s3-local/issues/492
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.