Skip to content

Commit 14c3070

Browse files
committed
update zipdownloader s3 to sdk v2
1 parent f073d08 commit 14c3070

2 files changed

Lines changed: 31 additions & 30 deletions

File tree

scripts/zipdownload/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
<artifactId>postgresql</artifactId>
2424
</dependency>
2525
<dependency>
26-
<groupId>com.amazonaws</groupId>
27-
<artifactId>aws-java-sdk-s3</artifactId>
26+
<groupId>software.amazon.awssdk</groupId>
27+
<artifactId>s3</artifactId>
28+
<!-- no version here as managed by BOM above! -->
2829
</dependency>
2930
</dependencies>
3031
<build>

scripts/zipdownload/src/main/java/edu/harvard/iq/dataverse/custom/service/util/DirectAccessUtil.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,31 @@
3131
import java.io.IOException;
3232
import java.io.InputStream;
3333

34+
package edu.harvard.iq.dataverse.custom.service.util;
35+
36+
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
37+
import software.amazon.awssdk.core.ResponseInputStream;
38+
import software.amazon.awssdk.regions.Region;
39+
import software.amazon.awssdk.services.s3.S3Client;
40+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
41+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
42+
import software.amazon.awssdk.services.s3.model.S3Exception;
43+
44+
import java.io.File;
45+
import java.io.FileInputStream;
46+
import java.io.IOException;
47+
import java.io.InputStream;
48+
3449
/**
3550
* Utility methods for directly accessing storage locations
3651
* Supports file system and S3.
3752
* (S3 has only been tested with AWS; non-standard auth may not be supported yet)
3853
*
3954
* @author Leonid Andreev
4055
*/
41-
public class DirectAccessUtil implements java.io.Serializable {
56+
public class DirectAccessUtil implements java.io.Serializable {
4257

43-
private AmazonS3 s3 = null;
58+
private S3Client s3 = null;
4459

4560
public InputStream openDirectAccess(String storageLocation) {
4661
InputStream inputStream = null;
@@ -57,31 +72,17 @@ public InputStream openDirectAccess(String storageLocation) {
5772
String bucket = storageLocation.substring(0, storageLocation.indexOf('/'));
5873
String key = storageLocation.substring(storageLocation.indexOf('/') + 1);
5974

60-
//System.out.println("bucket: "+bucket);
61-
//System.out.println("key: "+key);
62-
63-
/* commented-out code below is for looking up S3 metatadata
64-
properties, such as size, etc. prior to making an access call:
65-
ObjectMetadata objectMetadata = null;
66-
long fileSize = 0L;
67-
try {
68-
objectMetadata = s3.getObjectMetadata(bucket, key);
69-
fileSize = objectMetadata.getContentLength();
70-
//System.out.println("byte size: "+objectMetadata.getContentLength());
71-
} catch (SdkClientException sce) {
72-
System.err.println("Cannot get S3 object metadata " + key + " from bucket " + bucket);
73-
}*/
74-
7575
try {
76-
inputStream = s3.getObject(new GetObjectRequest(bucket, key)).getObjectContent();
77-
} catch (SdkClientException sce) {
76+
ResponseInputStream<GetObjectResponse> s3Object = s3.getObject(GetObjectRequest.builder()
77+
.bucket(bucket)
78+
.key(key)
79+
.build());
80+
inputStream = s3Object;
81+
} catch (S3Exception se) {
7882
System.err.println("Cannot get S3 object " + key + " from bucket " + bucket);
7983
}
8084

8185
} else if (storageLocation.startsWith("file://")) {
82-
// This could be a static method; since no reusable client/maintainable
83-
// state is required
84-
8586
storageLocation = storageLocation.substring(7);
8687

8788
try {
@@ -98,14 +99,13 @@ public InputStream openDirectAccess(String storageLocation) {
9899
private void createOrReuseAwsClient() {
99100
if (this.s3 == null) {
100101
try {
101-
AmazonS3ClientBuilder s3CB = AmazonS3ClientBuilder.standard();
102-
s3CB.setCredentials(new ProfileCredentialsProvider("default"));
103-
this.s3 = s3CB.build();
104-
102+
this.s3 = S3Client.builder()
103+
.region(Region.US_EAST_1) // You may want to make this configurable
104+
.credentialsProvider(ProfileCredentialsProvider.create("default"))
105+
.build();
105106
} catch (Exception e) {
106-
System.err.println("cannot instantiate an S3 client");
107+
System.err.println("Cannot instantiate an S3 client: " + e.getMessage());
107108
}
108109
}
109110
}
110-
111111
}

0 commit comments

Comments
 (0)