Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions storage/json-api/src/main/java/StorageSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions storage/json-api/src/test/java/StorageSampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down