英文:
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 "./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\n"}
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\n"}
答案1
得分: 0
accessKeyId
和 secretAccessKey
应该放在 credentials
属性中,如下所示:
const bucketProps = {
region: 'us-east-1',
endpoint: endPoint,
credentials: {
accessKeyId: 'qys4ddggd',
secretAccessKey: 'mysecretkryvalie'
}
}
Bucket
键不应存在于此 S3Client
配置对象中,只在创建命令时传递它。
至于 endpoint
,S3 文档 指出:
这仅用于使用自定义端点(例如,在使用本地版本的 S3 时)。
我不确定这是否是您需要的内容。
另外,虽然这对于测试而言不是问题,但最佳做法是不要将这些秘密密钥存储在代码中。您可以例如从 .env
文件中加载它们,使用 dotenv。
英文:
accessKeyId
and secretAccessKey
belong in the credentials
property like so:
const bucketProps = {
region: 'us-east-1',
endpoint: endPoint,
credentials: {
accessKeyId: 'qys4ddggd',
secretAccessKey: 'mysecretkryvalie'
}
}
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代码示例:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论