Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -66,9 +66,6 @@ public InputStream readBlob(String blobName) throws IOException {

@Override
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
if (blobExists(blobName)) {
throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
}
blobStore.writeBlob(buildKey(blobName), inputStream, blobSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -56,6 +57,7 @@
import java.util.stream.StreamSupport;

import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;

class GoogleCloudStorageBlobStore extends AbstractComponent implements BlobStore {

Expand Down Expand Up @@ -191,7 +193,7 @@ InputStream readBlob(String blobName) throws IOException {
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if ((e.getStatusCode() == HTTP_NOT_FOUND) || ((error != null) && (error.getCode() == HTTP_NOT_FOUND))) {
throw new NoSuchFileException(e.getMessage());
throw new NoSuchFileException(blobName, null, e.getMessage());
}
throw e;
}
Expand All @@ -209,8 +211,16 @@ void writeBlob(String blobName, InputStream inputStream, long blobSize) throws I
stream.setLength(blobSize);

Storage.Objects.Insert insert = client.objects().insert(bucket, null, stream);
insert.setIfGenerationMatch(0L); // ensures that the file does not already exist
insert.setName(blobName);
insert.execute();
try {
insert.execute();
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
if ((e.getStatusCode() == HTTP_PRECON_FAILED) || ((error != null) && (error.getCode() == HTTP_PRECON_FAILED))) {
throw new FileAlreadyExistsException(blobName, null, e.getMessage());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception must be thrown again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, thanks

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public StorageObject execute() throws IOException {
throw newBucketNotFoundException(getBucket());
}

if (getIfGenerationMatch() != null) {
if (getIfGenerationMatch() == 0L) {
if (blobs.containsKey(getName())) {
throw newPreconditionFailedException(getName());
}
} else {
throw new AssertionError("not implemented");
}
}

ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(insertStream.getInputStream(), out);
blobs.put(getName(), out.toByteArray());
Expand Down Expand Up @@ -236,6 +246,11 @@ private static GoogleJsonResponseException newObjectNotFoundException(final Stri
return new GoogleJsonResponseException(builder, new GoogleJsonError());
}

private static GoogleJsonResponseException newPreconditionFailedException(final String object) {
HttpResponseException.Builder builder = new HttpResponseException.Builder(412, "Precondition Failed: " + object, new HttpHeaders());
return new GoogleJsonResponseException(builder, new GoogleJsonError());
}

/**
* {@link MockedHttpTransport} extends the existing testing transport to analyze the content
* of {@link com.google.api.client.googleapis.batch.BatchRequest} and delete the appropriates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void testVerifyOverwriteFails() throws IOException {
final BytesArray bytesArray = new BytesArray(data);
writeBlob(container, blobName, bytesArray);
// should not be able to overwrite existing blob
expectThrows(IOException.class, () -> writeBlob(container, blobName, bytesArray));
expectThrows(FileAlreadyExistsException.class, () -> writeBlob(container, blobName, bytesArray));
container.deleteBlob(blobName);
writeBlob(container, blobName, bytesArray); // after deleting the previous blob, we should be able to write to it again
}
Expand Down