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

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

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:

import connProperties from "./myprops";
import { S3Client, GetObjectCommand, PutObjectCommand, PutObjectCommandInput } from "@aws-sdk/client-s3";
import {EndpointV2} from '@aws-sdk/types';
import AWS from "aws-sdk";

const connect = async() =>{

if(connProperties.isValidProps()){
 const endPoint: EndpointV2  = {url: new URL('http://my-s3-bucket-site.com')} as 
  EndpointV2 

 const bucketProps = {
   Bucket: 'data-log',
   region: 'us-east-1',
   endpoint: endPoint,
   accessKeyId: 'qys4ddggd',
   secretAccessKey: 'mysecretkryvalie'
 } 
 const command = new PutObjectCommand({
    Bucket: 'data-log',
    Key: "test-s3.txt",
    Body: "This is some text!",
  });

     const s3client = new S3Client(bucketProps);
     s3client.send(command); 
}else{
    console.log('Cannot send to S3 Bucket');
}
}

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


[default]
aws_access_key_id = <my-access-key>
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:


      import connProperties from &quot;./myprops&quot;;
      import { S3Client, GetObjectCommand, PutObjectCommand, PutObjectCommandInput } from 
                                                      &quot;@aws-sdk/client-s3&quot;;
      import {EndpointV2} from &#39;@aws-sdk/types&#39;
      import AWS from &quot;aws-sdk&quot;;

      const connect = async() =&gt;{


      if(connProperties.isValidProps()){
       const endPoint: EndpointV2  = {url: new URL(&#39;http://my-s3-bucket-site.com&#39;)} as 
        EndpointV2 
     
       const bucketProps = {
         Bucket: &#39;data-log&#39;,
         region: &#39;us-east-1&#39;,
         endpoint: endPoint,
         accessKeyId: &#39;qys4ddggd&#39;,
         secretAccessKey: &#39;mysecretkryvalie&#39;
       } 
       const command = new PutObjectCommand({
          Bucket: &#39;data-log&#39;,
          Key: &quot;test-s3.txt&quot;,
          Body: &quot;This is some text!&quot;,
        });
   
         const s3client = new S3Client(bucketProps);
         s3client.send(command); 
      }else{
        console.log(&#39;Cannot send to S3 Bucket&#39;);
      }
    }

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

  
  [default]
  aws_access_key_id = &lt;my-access-key&gt;
  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 属性中,如下所示:

const bucketProps = {
	region: 'us-east-1',
	endpoint: endPoint,
	credentials: {
		accessKeyId: 'qys4ddggd',
		secretAccessKey: 'mysecretkryvalie'
	}
}

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

至于 endpointS3 文档 指出:

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

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

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

英文:

accessKeyId and secretAccessKey belong in the credentials property like so:

const bucketProps = {
	region: &#39;us-east-1&#39;,
	endpoint: endPoint,
	credentials: {
		accessKeyId: &#39;qys4ddggd&#39;,
		secretAccessKey: &#39;mysecretkryvalie&#39;
	}
} 

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:

确定