diff --git a/storage/json-api/src/main/java/StorageSample.java b/storage/json-api/src/main/java/StorageSample.java index 092271eb8c0..474efc17292 100644 --- a/storage/json-api/src/main/java/StorageSample.java +++ b/storage/json-api/src/main/java/StorageSample.java @@ -20,9 +20,11 @@ import com.google.api.services.storage.model.Objects; import com.google.api.services.storage.model.StorageObject; -import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Arrays; @@ -91,13 +93,16 @@ public static Bucket getBucket(String bucketName) throws IOException, GeneralSec * * @param name the name of the destination object. * @param contentType the MIME type of the data. - * @param stream the data - for instance, you can use a FileInputStream to upload a file. + * @param file the file to upload. * @param bucketName the name of the bucket to create the object in. */ - public static void uploadStream( - String name, String contentType, InputStream stream, String bucketName) + public static void uploadFile( + String name, String contentType, File file, String bucketName) throws IOException, GeneralSecurityException { - InputStreamContent contentStream = new InputStreamContent(contentType, stream); + InputStreamContent contentStream = new InputStreamContent( + contentType, new FileInputStream(file)); + // Setting the length improves upload performance + contentStream.setLength(file.length()); StorageObject objectMetadata = new StorageObject() // Set the destination object name .setName(name) @@ -161,11 +166,13 @@ public static void main(String[] args) { } - // Upload a stream to the bucket. This could very well be a file. - uploadStream( - TEST_FILENAME, "text/plain", - new ByteArrayInputStream("Test of json storage sample".getBytes()), - bucketName); + // Create a temp file to upload + Path tempPath = Files.createTempFile("StorageSample", "txt"); + Files.write(tempPath, "Sample file".getBytes()); + File tempFile = tempPath.toFile(); + tempFile.deleteOnExit(); + // Upload it + uploadFile(TEST_FILENAME, "text/plain", tempFile, bucketName); // Now delete the file deleteObject(TEST_FILENAME, bucketName); diff --git a/storage/json-api/src/test/java/StorageSampleTest.java b/storage/json-api/src/test/java/StorageSampleTest.java index e0989203f7d..572d88ebe30 100644 --- a/storage/json-api/src/test/java/StorageSampleTest.java +++ b/storage/json-api/src/test/java/StorageSampleTest.java @@ -23,7 +23,9 @@ import org.junit.Test; -import java.io.ByteArrayInputStream; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; @@ -46,12 +48,14 @@ public void testGetBucket() throws Exception { @Test public void testUploadDelete() throws Exception { - StorageSample.uploadStream( - TEST_OBJECT, "text/plain", - new ByteArrayInputStream( - ("This object is uploaded and deleted as part of the " - + "StorageSampleTest integration test.").getBytes()), - BUCKET); + // Create a temp file to upload + Path tempPath = Files.createTempFile("StorageSampleTest", "txt"); + Files.write(tempPath, ("This object is uploaded and deleted as part of the " + + "StorageSampleTest integration test.").getBytes()); + File tempFile = tempPath.toFile(); + tempFile.deleteOnExit(); + + StorageSample.uploadFile(TEST_OBJECT, "text/plain", tempFile, BUCKET); try { // Verify that the object was created