如何使用Nodejs版本3的SDK提供凭证连接到S3存储桶

huangapple go评论108阅读模式
英文:

How to connect to S3 Bucket providing credentials using Nodejs version 3 sdk

问题

I am trying to send some simple text to a S3 Bucket. I have attempted to create a connection to the bucket using NodeJS and the V3 SDK. Unfortunately, I get an exception indicating I am missing a credential provider. Here is my code:

  1. import connProperties from "./myprops";
  2. import { S3Client, GetObjectCommand, PutObjectCommand, PutObjectCommandInput } from "@aws-sdk/client-s3";
  3. import {EndpointV2} from '@aws-sdk/types';
  4. import AWS from "aws-sdk";
  5. const connect = async() =>{
  6. if(connProperties.isValidProps()){
  7. const endPoint: EndpointV2 = {url: new URL('http://my-s3-bucket-site.com')} as
  8. EndpointV2
  9. const bucketProps = {
  10. Bucket: 'data-log',
  11. region: 'us-east-1',
  12. endpoint: endPoint,
  13. accessKeyId: 'qys4ddggd',
  14. secretAccessKey: 'mysecretkryvalie'
  15. }
  16. const command = new PutObjectCommand({
  17. Bucket: 'data-log',
  18. Key: "test-s3.txt",
  19. Body: "This is some text!",
  20. });
  21. const s3client = new S3Client(bucketProps);
  22. s3client.send(command);
  23. }else{
  24. console.log('Cannot send to S3 Bucket');
  25. }
  26. }

I have no idea how to provide the credentials, the document has no examples which do that. Here is the exception I get:

{"timestamp":"2023-04-13T15:16:21.347Z","message":"This exception is: CredentialsProviderError: Could not load credentials from any providers"}

This did not work for me either: ~/.aws/credentials

  1. [default]
  2. aws_access_key_id = <my-access-key>
  3. aws_secret_access_key = <my-secret-key>

The error is {"timestamp":"2023-04-13T16:17:04.166Z","message":"The output Error: unable to verify the first certificate"}

英文:

I am trying to send some simple text to a S3 Bucket. I have attempted to create a connection to the bucket using NodeJS and the V3 SDK. Unfortunately, I get an exception indicating I am missing a credential provider. Here is my code:

  1. import connProperties from &quot;./myprops&quot;;
  2. import { S3Client, GetObjectCommand, PutObjectCommand, PutObjectCommandInput } from
  3. &quot;@aws-sdk/client-s3&quot;;
  4. import {EndpointV2} from &#39;@aws-sdk/types&#39;
  5. import AWS from &quot;aws-sdk&quot;;
  6. const connect = async() =&gt;{
  7. if(connProperties.isValidProps()){
  8. const endPoint: EndpointV2 = {url: new URL(&#39;http://my-s3-bucket-site.com&#39;)} as
  9. EndpointV2
  10. const bucketProps = {
  11. Bucket: &#39;data-log&#39;,
  12. region: &#39;us-east-1&#39;,
  13. endpoint: endPoint,
  14. accessKeyId: &#39;qys4ddggd&#39;,
  15. secretAccessKey: &#39;mysecretkryvalie&#39;
  16. }
  17. const command = new PutObjectCommand({
  18. Bucket: &#39;data-log&#39;,
  19. Key: &quot;test-s3.txt&quot;,
  20. Body: &quot;This is some text!&quot;,
  21. });
  22. const s3client = new S3Client(bucketProps);
  23. s3client.send(command);
  24. }else{
  25. console.log(&#39;Cannot send to S3 Bucket&#39;);
  26. }
  27. }

I have no idea how to provide the credentials, the document has no examples which do that. Here is the exception I get:

{"timestamp":"2023-04-13T15:16:21.347Z","message":"This exception is: CredentialsProviderError: Could not load credentials from any providers\n"}

This did not work for me either: ~/.aws/credentials

  1. [default]
  2. aws_access_key_id = &lt;my-access-key&gt;
  3. aws_secret_access_key = &lt;my-secret-key&gt;

The error is {"timestamp":"2023-04-13T16:17:04.166Z","message":"The output Error: unable to verify the first certificate\n"}

答案1

得分: 0

accessKeyIdsecretAccessKey 应该放在 credentials 属性中,如下所示:

  1. const bucketProps = {
  2. region: 'us-east-1',
  3. endpoint: endPoint,
  4. credentials: {
  5. accessKeyId: 'qys4ddggd',
  6. secretAccessKey: 'mysecretkryvalie'
  7. }
  8. }

Bucket 键不应存在于此 S3Client 配置对象中,只在创建命令时传递它。

至于 endpointS3 文档 指出:

这仅用于使用自定义端点(例如,在使用本地版本的 S3 时)。

我不确定这是否是您需要的内容。

另外,虽然这对于测试而言不是问题,但最佳做法是不要将这些秘密密钥存储在代码中。您可以例如从 .env 文件中加载它们,使用 dotenv

英文:

accessKeyId and secretAccessKey belong in the credentials property like so:

  1. const bucketProps = {
  2. region: &#39;us-east-1&#39;,
  3. endpoint: endPoint,
  4. credentials: {
  5. accessKeyId: &#39;qys4ddggd&#39;,
  6. secretAccessKey: &#39;mysecretkryvalie&#39;
  7. }
  8. }

Also the Bucket key does not exist in this S3Client config object, you only pass it when creating a command.

As for the endpoint, the S3 docs state:

> This is only for using a custom endpoint (for example, when using a local version of S3).

I'm not sure if that's what you need.

And as a side note, while this is not an issue for testing, it's a best practice to not store these secret keys in code. You can for example load them from a .env file using dotenv.

答案2

得分: 0

这些信息都在AWS SDK JavaScript指南中有详细记录。我不确定你正在查看哪个AWS文档,但这里有关于从共享凭证文件加载Node.js凭证的文档。

在使用AWS SDK for JavaScript V3时,建议参考此开发者指南。还可以参考此GitHub链接获取JavaScript代码示例:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/s3/scenarios/basic.js

英文:

This is all documented in the AWS SDK Guide for JavaScript. I am not sure which AWS Doc you are looking at, but this is docuemnted here:

Loading credentials in Node.js from the shared credentials file

Its a good idea to use this DEV guide when working with AWS SDK for JavaScript V3.

Refer to this Github for JS code examples:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/s3/scenarios/basic.js

huangapple
  • 本文由 发表于 2023年4月13日 23:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定