skip to Main Content

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 providersn"}

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 certificaten"}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search