-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3Utility.java
69 lines (57 loc) · 2.18 KB
/
S3Utility.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.io.File;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class S3Utility
{
private AWSCredentials credentials = null;
private String bucketName;
private String folder_name;
public S3Utility(String bucketname,String folder_name)
{
this.bucketName = bucketname;
this.folder_name = folder_name;
}
public S3Utility ()
{
this.bucketName = "";
}
public void uploadFile(String uploadFileName) {
try {
credentials = new ProfileCredentialsProvider("default")
.getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct ",
e);
}
AmazonS3 s3 = new AmazonS3Client(credentials);
try {
System.out.println("Uploading a new object to S3 from a file\n");
File file = new File(uploadFileName);
s3.putObject(new PutObjectRequest(bucketName, folder_name + "/rotten_" + uploadFileName+".txt", file));
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which "
+ "means your request made it "
+ "to Amazon S3, but was rejected with an error response"
+ " for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which "
+ "means the client encountered "
+ "an internal error while trying to "
+ "communicate with S3, "
+ "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
}
}