diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 2cf693e83f..94b3387768 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -230,6 +230,10 @@ public Blob createFrom(BlobInfo blobInfo, Path path, int bufferSize, BlobWriteOp if (Files.isDirectory(path)) { throw new StorageException(0, path + " is a directory"); } + long size = Files.size(path); + if (size == 0L) { + return create(blobInfo, null, options); + } Opts opts = Opts.unwrap(options).resolveFrom(blobInfo); final Map optionsMap = opts.getRpcOptions(); BlobInfo.Builder builder = blobInfo.toBuilder().setMd5(null).setCrc32c(null); @@ -251,7 +255,6 @@ public Blob createFrom(BlobInfo blobInfo, Path path, int bufferSize, BlobWriteOp getOptions().asRetryDependencies(), retryAlgorithmManager.idempotent(), jsonResumableWrite); - long size = Files.size(path); HttpContentRange contentRange = HttpContentRange.of(ByteRangeSpec.relativeLength(0L, size), size); ResumableOperationResult put = diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java index e6f0607d7e..99ed548098 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java @@ -74,10 +74,12 @@ import com.google.common.primitives.Ints; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; +import java.nio.file.Paths; import java.security.Key; import java.util.Arrays; import java.util.HashMap; @@ -197,6 +199,21 @@ public void testCreateEmptyBlob() { assertArrayEquals(new byte[0], readBytes); } + @Test + public void testZeroByteFileUpload() throws Exception { + String blobName = generator.randomObjectName(); + BlobId blobId = BlobId.of(bucket.getName(), blobName); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + + File zeroByteFile = File.createTempFile("zerobyte", null); + zeroByteFile.deleteOnExit(); + + storage.createFrom(blobInfo, Paths.get(zeroByteFile.getAbsolutePath())); + + byte[] readBytes = storage.readAllBytes(bucket.getName(), blobName); + assertArrayEquals(new byte[0], readBytes); + } + @Test @SuppressWarnings({"unchecked", "deprecation"}) public void testCreateBlobStream() {