Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if i use aws java v2 sdk putobject fail #812

Open
lygn128 opened this issue Jun 8, 2023 · 2 comments
Open

if i use aws java v2 sdk putobject fail #812

lygn128 opened this issue Jun 8, 2023 · 2 comments

Comments

@lygn128
Copy link

lygn128 commented Jun 8, 2023

if i use aws cpp sdk putobject suc;
if i use aws golang sdk putobject suc;
but if i use aws java sdk then fail;

@cmdreay
Copy link

cmdreay commented Jun 8, 2023

use async method will be suc

AwsCredentials credentials = AwsBasicCredentials.create(accessKeyId, accessKeySecret);
S3AsyncClient s3AsyncClient = S3AsyncClient.crtBuilder()
        .credentialsProvider(() -> credentials)
        .region(Region.US_EAST_1)
        .endpointOverride(URI.create(endpoint))
        .targetThroughputInGbps(20.0)
        .checksumValidationEnabled(false)
        .minimumPartSizeInBytes(5 * MB)
        .build();

@cmdreay
Copy link

cmdreay commented Jun 8, 2023

two version
// 2.x
<aws.java.sdk.version>2.20.43</aws.java.sdk.version>

  public static  void main(String[] args) {
  
      String bucketName = "test_v3";
      String accessKeyId = "user";
      String accessKeySecret = "12345abc";
      String endpoint = "http://172.168.6.119:4568";
  
  
      S3Configuration s3Config = S3Configuration.builder().pathStyleAccessEnabled(true).chunkedEncodingEnabled(false).build();
      SdkHttpClient urlHttpClient = UrlConnectionHttpClient.builder()
              .socketTimeout(Duration.ofMinutes(5))
              .build();
      AwsCredentials credentials = AwsBasicCredentials.create(accessKeyId, accessKeySecret);
  
      Path uploadPath = Paths.get("e:/aa.pdf");
      final String objectKey = "a11206.pdf";//uploadPath.toFile().getName();
      
      S3Client s3Client = S3Client.builder()
              .credentialsProvider(() -> credentials)
              .region(Region.US_EAST_1)
              .defaultsMode(DefaultsMode.IN_REGION)
              .httpClient(urlHttpClient)
              .serviceConfiguration(s3Config)
              .endpointOverride(URI.create(endpoint))
              .build();
  
      Map<String, String> metadata = new HashMap<>();
      metadata.put("x-amz-meta-location", "Beijing");
      PutObjectRequest pubObjectRequest = PutObjectRequest.builder()
              .bucket(bucketName)
              .key(objectKey)
              .metadata(metadata)
              .build();
  
      PutObjectResponse putObjectResponse = s3Client.putObject(pubObjectRequest, uploadPath);
  
      System.out.println(putObjectResponse.eTag());
  
  }

// 1.x
<com.amazonaws.version>1.12.448</com.amazonaws.version>

public static void main(String[] args) {

final String bucketName = "test_v3";
String accessKeyId = "user";
String accessKeySecret = "12345abc";
String endpoint = "http://172.168.6.119:4568";

BasicAWSCredentials credentials = new BasicAWSCredentials(accessKeyId, accessKeySecret);
AmazonS3Client s3Client = new AmazonS3Client(credentials);
s3Client.setEndpoint(endpoint);
s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true).withChunkedEncodingDisabled(true));

PutObjectRequest objectRequest = new PutObjectRequest(bucketName, "bbbb16.pdf", new File("E:/aa.pdf"));
s3Client.putObject(objectRequest);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants