Skip to content

Commit 0c05107

Browse files
fix: Add an exception to zero byte uploads on CreateFrom (#2342) (#2371)
* fix: Add an exception to zero byte uploads on CreateFrom * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 903622c commit 0c05107

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ public Blob createFrom(BlobInfo blobInfo, Path path, int bufferSize, BlobWriteOp
230230
if (Files.isDirectory(path)) {
231231
throw new StorageException(0, path + " is a directory");
232232
}
233+
long size = Files.size(path);
234+
if (size == 0L) {
235+
return create(blobInfo, null, options);
236+
}
233237
Opts<ObjectTargetOpt> opts = Opts.unwrap(options).resolveFrom(blobInfo);
234238
final Map<StorageRpc.Option, ?> optionsMap = opts.getRpcOptions();
235239
BlobInfo.Builder builder = blobInfo.toBuilder().setMd5(null).setCrc32c(null);
@@ -251,7 +255,6 @@ public Blob createFrom(BlobInfo blobInfo, Path path, int bufferSize, BlobWriteOp
251255
getOptions().asRetryDependencies(),
252256
retryAlgorithmManager.idempotent(),
253257
jsonResumableWrite);
254-
long size = Files.size(path);
255258
HttpContentRange contentRange =
256259
HttpContentRange.of(ByteRangeSpec.relativeLength(0L, size), size);
257260
ResumableOperationResult<StorageObject> put =

google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITObjectTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@
7474
import com.google.common.primitives.Ints;
7575
import java.io.ByteArrayInputStream;
7676
import java.io.ByteArrayOutputStream;
77+
import java.io.File;
7778
import java.io.IOException;
7879
import java.io.InputStream;
7980
import java.nio.ByteBuffer;
8081
import java.nio.channels.Channels;
82+
import java.nio.file.Paths;
8183
import java.security.Key;
8284
import java.util.Arrays;
8385
import java.util.HashMap;
@@ -197,6 +199,21 @@ public void testCreateEmptyBlob() {
197199
assertArrayEquals(new byte[0], readBytes);
198200
}
199201

202+
@Test
203+
public void testZeroByteFileUpload() throws Exception {
204+
String blobName = generator.randomObjectName();
205+
BlobId blobId = BlobId.of(bucket.getName(), blobName);
206+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
207+
208+
File zeroByteFile = File.createTempFile("zerobyte", null);
209+
zeroByteFile.deleteOnExit();
210+
211+
storage.createFrom(blobInfo, Paths.get(zeroByteFile.getAbsolutePath()));
212+
213+
byte[] readBytes = storage.readAllBytes(bucket.getName(), blobName);
214+
assertArrayEquals(new byte[0], readBytes);
215+
}
216+
200217
@Test
201218
@SuppressWarnings({"unchecked", "deprecation"})
202219
public void testCreateBlobStream() {

0 commit comments

Comments
 (0)