Upload an object by using a stream
Note: This example assumes that the Bucket sample exists.
Example: Upload an Object from an I/O Stream to a Bucket in a system using the s3Client.putObject() method
AmazonS3 s3Client = new AmazonS3Client();
s3Client.setEndpoint("https://systemname.example.com");
String obj = "An example"; 1
ByteArrayOutputStream theBytes = new ByteArrayOutputStream(); 2
ObjectOutputStream serializer = new ObjectOutputStream(theBytes); 3
serializer.writeObject(obj); 4
serializer.flush();
serializer.close();
InputStream stream = new ByteArrayInputStream(theBytes.toByteArray()); 5
ObjectMetadata metadata = new ObjectMetadata(); 6
metadata.setContentType("application/x-java-serialized-object"); 7
metadata.setContentLength(theBytes.size()); 8
s3Client.putObject(
"sample", 9
"serialized-object", 10
stream, 11
metadata 12
);
1 An object to be stored.
2 Create a new output stream to store the object data.
3 Set the object data to be serialized.
4 Serialize the object data.
5 Convert the serialized object data into a new input stream to store.
6 Define the metadata about this object to be stored with it.
7 Set the metadata for the object being written.
8 Set metadata for the length of the data stream.
9 The name of the Bucket to which the Object is being written.
10 The name of the Object being written.
11 The name of the data stream writing the Object.
12 The metadata for the Object being written.