Skip to content

Commit

Permalink
fix: Add an exception to zero byte uploads on CreateFrom (#2342) (#2371)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JesseLovelace and gcf-owl-bot[bot] committed Jan 22, 2024
1 parent 903622c commit 0c05107
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Expand Up @@ -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<ObjectTargetOpt> opts = Opts.unwrap(options).resolveFrom(blobInfo);
final Map<StorageRpc.Option, ?> optionsMap = opts.getRpcOptions();
BlobInfo.Builder builder = blobInfo.toBuilder().setMd5(null).setCrc32c(null);
Expand All @@ -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<StorageObject> put =
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0c05107

Please sign in to comment.