Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add an exception to zero byte uploads on CreateFrom #2342

Merged
merged 2 commits into from
Jan 2, 2024
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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