1

If you run the code below, the following error occurred. I want to know the solution. I want to see a sample of how to do the AWS S3 configuration part. Source code

S3TransferManager transferManager = S3TransferManager.create();
UploadFileRequest uploadFileRequest = UploadFileRequest.builder()
                                 .putObjectRequest(req -> req.bucket(BUCKET).key(downloadFileName))
                                 .addTransferListener(LoggingTransferListener.create())
                                 .source(Paths.get(filePath_Extract))
                                 .build();
FileUpload upload = transferManager.uploadFile(uploadFileRequest);
// Wait for the transfer to complete
upload.completionFuture().join();

Error

software.amazon.awssdk.core.exception.SdkClientException: Unable to load credentials from any of the providers in the chain AwsCredentialsProviderChain(credentialsProviders=[SystemPropertyCredentialsProvider(), EnvironmentVariableCredentialsProvider(), WebIdentityTokenCredentialsProvider(), ProfileCredentialsProvider
.
.
.


SystemPropertyCredentialsProvider(): Unable to load credentials from system settings. Access key must be specified either via environment variable (AWS_ACCESS_KEY_ID) or system property (aws.accessKeyId).
    , EnvironmentVariableCredentialsProvider(): Unable to load credentials from system settings. Access key must be specified either via environment variable (AWS_ACCESS_KEY_ID) or system property (aws.accessKeyId).
    , WebIdentityTokenCredentialsProvider(): Either the environment variable AWS_WEB_IDENTITY_TOKEN_FILE or the javaproperty aws.webIdentityTokenFile must be set.

    To use Sso related properties in the '112903851907_CREWODS' profile
    , the 'sso' service module must be on the class path.
    , ContainerCredentialsProvider(): Cannot fetch credentials from container - neither AWS_CONTAINER_CREDENTIALS_FULL_URI or AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variables are set.
    , InstanceProfileCredentialsProvider(): Failed to load credentials from IMDS.

I tried

S3TransferManager transferManager = S3TransferManager.create();
UploadFileRequest uploadFileRequest = UploadFileRequest.builder()
                                 .putObjectRequest(req -> req.bucket(BUCKET).key(downloadFileName))
                                 .addTransferListener(LoggingTransferListener.create())
                                 .source(Paths.get(filePath_Extract))
                                 .build();
FileUpload upload = transferManager.uploadFile(uploadFileRequest);
// Wait for the transfer to complete
upload.completionFuture().join();

I expect the local files to be uploaded to the AWS S3 bucket.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
이상빈
  • 11
  • 2

1 Answers1

0

To make requests to AWS S3, you must supply AWS temporary credentials. You can achieve this in few different ways:

  • default credential provider chain (recommended).

  • specific credential provider or provider chain (or create your own).

  • supply the temporary credentials yourself in code.

More information on how SDK is looking for temporary credentials can be found here: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default

Here is example code:

S3AsyncClient s3AsyncClient = S3AsyncClient
        .builder()
        .credentialsProvider(DefaultCredentialsProvider.create())
        .region(Region.EU_CENTRAL_1)
        .build();

S3TransferManager transferManager = S3TransferManager
        .builder()
        .s3Client(s3AsyncClient)
        .build();

You can also choose the provider yourself depending on the profile with which the application works. Read the documentation and look at the AwsCredentialsProvider interface and see what implementations it has. Choose the one you need. If it still doesn't work, then there is a problem with the AWS credentials / profile configuration.

KSs
  • 420
  • 3
  • 9
  • If this answer best solved your issue, then consider marking it as correct to help guide others with the same issue in the future. – KSs May 01 '23 at 17:10