Skip to content

Commit 1d2ff14

Browse files
author
Jerjou Cheng
committed
Specify file length, for performance.
1 parent 58ba309 commit 1d2ff14

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

storage/json-api/src/main/java/StorageSample.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
import com.google.api.services.storage.model.Objects;
2121
import com.google.api.services.storage.model.StorageObject;
2222

23-
import java.io.ByteArrayInputStream;
23+
import java.io.File;
24+
import java.io.FileInputStream;
2425
import java.io.IOException;
25-
import java.io.InputStream;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
2628
import java.security.GeneralSecurityException;
2729
import java.util.ArrayList;
2830
import java.util.Arrays;
@@ -91,13 +93,16 @@ public static Bucket getBucket(String bucketName) throws IOException, GeneralSec
9193
*
9294
* @param name the name of the destination object.
9395
* @param contentType the MIME type of the data.
94-
* @param stream the data - for instance, you can use a FileInputStream to upload a file.
96+
* @param file the file to upload.
9597
* @param bucketName the name of the bucket to create the object in.
9698
*/
97-
public static void uploadStream(
98-
String name, String contentType, InputStream stream, String bucketName)
99+
public static void uploadFile(
100+
String name, String contentType, File file, String bucketName)
99101
throws IOException, GeneralSecurityException {
100-
InputStreamContent contentStream = new InputStreamContent(contentType, stream);
102+
InputStreamContent contentStream = new InputStreamContent(
103+
contentType, new FileInputStream(file));
104+
// Setting the length improves upload performance
105+
contentStream.setLength(file.length());
101106
StorageObject objectMetadata = new StorageObject()
102107
// Set the destination object name
103108
.setName(name)
@@ -161,11 +166,13 @@ public static void main(String[] args) {
161166
}
162167

163168

164-
// Upload a stream to the bucket. This could very well be a file.
165-
uploadStream(
166-
TEST_FILENAME, "text/plain",
167-
new ByteArrayInputStream("Test of json storage sample".getBytes()),
168-
bucketName);
169+
// Create a temp file to upload
170+
Path tempPath = Files.createTempFile("StorageSample", "txt");
171+
Files.write(tempPath, "Sample file".getBytes());
172+
File tempFile = tempPath.toFile();
173+
tempFile.deleteOnExit();
174+
// Upload it
175+
uploadFile(TEST_FILENAME, "text/plain", tempFile, bucketName);
169176

170177
// Now delete the file
171178
deleteObject(TEST_FILENAME, bucketName);

storage/json-api/src/test/java/StorageSampleTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
import org.junit.Test;
2525

26-
import java.io.ByteArrayInputStream;
26+
import java.io.File;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
2729
import java.util.List;
2830
import java.util.stream.Collectors;
2931

@@ -46,12 +48,14 @@ public void testGetBucket() throws Exception {
4648

4749
@Test
4850
public void testUploadDelete() throws Exception {
49-
StorageSample.uploadStream(
50-
TEST_OBJECT, "text/plain",
51-
new ByteArrayInputStream(
52-
("This object is uploaded and deleted as part of the "
53-
+ "StorageSampleTest integration test.").getBytes()),
54-
BUCKET);
51+
// Create a temp file to upload
52+
Path tempPath = Files.createTempFile("StorageSampleTest", "txt");
53+
Files.write(tempPath, ("This object is uploaded and deleted as part of the "
54+
+ "StorageSampleTest integration test.").getBytes());
55+
File tempFile = tempPath.toFile();
56+
tempFile.deleteOnExit();
57+
58+
StorageSample.uploadFile(TEST_OBJECT, "text/plain", tempFile, BUCKET);
5559

5660
try {
5761
// Verify that the object was created

0 commit comments

Comments
 (0)