79309486

Date: 2024-12-26 12:36:28
Score: 0.5
Natty:
Report link

The issue must be with the region property, the aws-region you created your bucket and the aws-region you're trying to access must be different.

package test_mvn;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;

public class S3Test {

    public static void main(String[] args) throws IOException {
        System.out.println(new S3Test().putS3Object("uploads/s3upload.csv", "text/csv", 
                Files.readAllBytes(Paths.get("/Users/sridharsivaraman/Downloads/s3upload.csv"))));
    }
    
    // NOTE: this should be available properly
    String bucketName = "test-bucket-sridhar";
    
    private String putS3Object(String objectKey, String mimeType, byte[] content) {

         S3Client s3 = S3Client.builder()
                    .region(Region.US_EAST_1) // Make sure this is the region where your bucket is located
                    .build();

                    System.out.println("adding " + objectKey + " - " + mimeType + " - to " + bucketName);
                    try {
                        PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
                                        .bucket(bucketName)
                                        .contentType(mimeType)
                                        .key(objectKey)
                                        .acl(ObjectCannedACL.PUBLIC_READ)
                                        .build(),
                                RequestBody.fromBytes(content));
                        
                        return response.eTag();
                    } catch (S3Exception e) {
                        e.printStackTrace(System.err);
                    }
                    return "";
                }


}

this is a sample I ran from my local with the following output

adding uploads/s3upload.csv - text/csv - to test-bucket-sridhar
"c4030959207ba4d4512fa9a3103f83e4"

and public s3 URL https://test-bucket-sridhar.s3.us-east-1.amazonaws.com/uploads/s3upload.csv with etag - [c4030959207ba4d4512fa9a3103f83e4] - same as one from output

s3_output_partial

Reasons:
  • Probably link only (1):
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
Posted by: Sridhar