Skip to content
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