-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3Upload.java
More file actions
50 lines (42 loc) · 1.58 KB
/
Copy pathS3Upload.java
File metadata and controls
50 lines (42 loc) · 1.58 KB
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
import com.amazonaws.AmazonServiceException;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import java.io.*;
/**
* Upload a file to an Amazon S3 bucket.
* <p>
* This code expects that you have AWS credentials set up per:
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
*/
public class S3Upload {
public static void main(String[] args) {
String bucket_name = "<FMI1>";
String file_path = "<FMI2>";
String key_name = "<FMI3>";
String fileContents = readFileContents();
System.out.format("Uploading %s to S3 bucket %s...\n", file_path, bucket_name);
final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
try {
s3.putObject(bucket_name, key_name, new File(fileContents));
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
private static String readFileContents() {
String fileContents = "";
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\<FMI2>"))) {
while (true) {
fileContents += reader.readLine();
if (fileContents == null || fileContents.equals("")) {
break;
}
}
} catch (IOException e) {
System.out.println("Something went wrong");
}
return fileContents;
}
}