use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class HttpStorageRpc method read.
@Override
public Tuple<String, byte[]> read(StorageObject from, Map<Option, ?> options, long position, int bytes) {
try {
Get req = storage.objects().get(from.getBucket(), from.getName()).setGeneration(from.getGeneration()).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options));
checkArgument(position >= 0, "Position should be non-negative, is %d", position);
StringBuilder range = new StringBuilder();
range.append("bytes=").append(position).append("-").append(position + bytes - 1);
HttpHeaders requestHeaders = req.getRequestHeaders();
requestHeaders.setRange(range.toString());
setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, options);
ByteArrayOutputStream output = new ByteArrayOutputStream(bytes);
HttpResponse httpResponse = req.executeMedia();
// todo(mziccard) remove when
// https://github.com/GoogleCloudPlatform/google-cloud-java/issues/982 is fixed
String contentEncoding = httpResponse.getContentEncoding();
if (contentEncoding != null && contentEncoding.contains("gzip")) {
try {
Field responseField = httpResponse.getClass().getDeclaredField("response");
responseField.setAccessible(true);
LowLevelHttpResponse lowLevelHttpResponse = (LowLevelHttpResponse) responseField.get(httpResponse);
IOUtils.copy(lowLevelHttpResponse.getContent(), output);
} catch (IllegalAccessException | NoSuchFieldException ex) {
throw new StorageException(BaseServiceException.UNKNOWN_CODE, "Error parsing gzip response", ex);
}
} else {
httpResponse.download(output);
}
String etag = req.getLastResponseHeaders().getETag();
return Tuple.of(etag, output.toByteArray());
} catch (IOException ex) {
StorageException serviceException = translate(ex);
if (serviceException.getCode() == HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE) {
return Tuple.of(null, new byte[0]);
}
throw serviceException;
}
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITBlobSnippets method testBlob.
@Test
public void testBlob() throws IOException {
BlobSnippets blobSnippets = new BlobSnippets(blob);
assertTrue(blobSnippets.exists());
assertArrayEquals(EMPTY_CONTENT, blobSnippets.getContent());
try {
assertNotNull(blobSnippets.reload());
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
Blob updatedBlob = blobSnippets.update();
assertEquals(ImmutableMap.of("key", "value"), updatedBlob.getMetadata());
Blob copiedBlob = blobSnippets.copyToStrings(BUCKET, "copyBlob");
assertNotNull(copiedBlob);
copiedBlob.delete();
copiedBlob = blobSnippets.copyToId(BUCKET, "copyBlob");
assertNotNull(copiedBlob);
copiedBlob.delete();
copiedBlob = blobSnippets.copyToBucket(BUCKET);
assertNotNull(copiedBlob);
copiedBlob.delete();
blobSnippets.reload();
blobSnippets.writer();
URL signedUrl = blobSnippets.signUrl();
URLConnection connection = signedUrl.openConnection();
byte[] readBytes = new byte[CONTENT.length];
try (InputStream responseStream = connection.getInputStream()) {
assertEquals(CONTENT.length, responseStream.read(readBytes));
assertArrayEquals(CONTENT, readBytes);
}
signedUrl = blobSnippets.signUrlWithSigner(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
connection = signedUrl.openConnection();
try (InputStream responseStream = connection.getInputStream()) {
assertEquals(CONTENT.length, responseStream.read(readBytes));
assertArrayEquals(CONTENT, readBytes);
}
assertFalse(blobSnippets.delete());
blobSnippets = new BlobSnippets(storage.get(blob.getBucket(), blob.getName()));
byte[] subcontent = blobSnippets.readContentRange(1, 8);
assertArrayEquals("ello, W".getBytes(UTF_8), subcontent);
assertNull(blobSnippets.getAcl());
assertNotNull(blobSnippets.createAcl());
Acl updatedAcl = blobSnippets.updateAcl();
assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(blobSnippets.listAcls());
assertTrue(acls.contains(updatedAcl));
assertTrue(blobSnippets.deleteAcl());
assertNull(blobSnippets.getAcl());
storage.delete(BlobId.of(BUCKET, BLOB));
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testBlobAcl.
@Test
public void testBlobAcl() {
String blobName = "test-blob-acl";
BlobId blobId = BlobId.of(BUCKET, "test-blob-acl");
BlobInfo blob = BlobInfo.newBuilder(blobId).build();
Blob createdBlob = storage.create(blob);
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertNotNull(storageSnippets.createBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
Acl updatedAcl = storageSnippets.updateBlobAcl(BUCKET, blobName, createdBlob.getGeneration());
assertEquals(Acl.Role.OWNER, updatedAcl.getRole());
Set<Acl> acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL));
storage.createAcl(BlobId.of(BUCKET, blobName), Acl.of(new User(USER_EMAIL), Role.READER));
Acl userAcl = storageSnippets.getBlobAcl(BUCKET, blobName, USER_EMAIL);
assertNotNull(userAcl);
assertEquals(USER_EMAIL, ((User) userAcl.getEntity()).getEmail());
updatedAcl = storageSnippets.blobToPublicRead(BUCKET, blobName, createdBlob.getGeneration());
assertEquals(Acl.Role.READER, updatedAcl.getRole());
assertEquals(User.ofAllUsers(), updatedAcl.getEntity());
acls = Sets.newHashSet(storageSnippets.listBlobAcls(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(acls.contains(updatedAcl));
assertNotNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertTrue(storageSnippets.deleteBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
// test non-existing blob
String nonExistingBlob = "test-blob-acl";
assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, -1L));
assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, -1L));
try {
storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, -1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, -1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
try {
storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, -1L);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class ITStorageSnippets method testCreateCopyAndGetBlob.
@Test
public void testCreateCopyAndGetBlob() {
String blobName = "test-create-copy-get-blob";
Blob blob = storageSnippets.createBlobFromByteArray(BUCKET, blobName);
assertNotNull(blob);
Blob copiedBlob = storageSnippets.copyBlobInChunks(BUCKET, blobName, "copy-blob");
assertNotNull(copiedBlob);
try {
storageSnippets.getBlobFromIdWithMetageneration(BUCKET, blobName, -1);
fail("Expected StorageException to be thrown");
} catch (StorageException ex) {
// expected
}
assertTrue(storageSnippets.deleteBlobFromIdWithGeneration(BUCKET, blobName, blob.getGeneration()));
copiedBlob.delete();
}
use of com.google.cloud.storage.StorageException in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProvider method newWriteChannel.
private SeekableByteChannel newWriteChannel(Path path, Set<? extends OpenOption> options) throws IOException {
initStorage();
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
BlobId file = cloudPath.getBlobId();
BlobInfo.Builder infoBuilder = BlobInfo.newBuilder(file);
List<Storage.BlobWriteOption> writeOptions = new ArrayList<>();
List<Acl> acls = new ArrayList<>();
HashMap<String, String> metas = new HashMap<>();
for (OpenOption option : options) {
if (option instanceof OptionMimeType) {
infoBuilder.setContentType(((OptionMimeType) option).mimeType());
} else if (option instanceof OptionCacheControl) {
infoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
} else if (option instanceof OptionContentDisposition) {
infoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
} else if (option instanceof OptionContentEncoding) {
infoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
} else if (option instanceof OptionUserMetadata) {
OptionUserMetadata opMeta = (OptionUserMetadata) option;
metas.put(opMeta.key(), opMeta.value());
} else if (option instanceof OptionAcl) {
acls.add(((OptionAcl) option).acl());
} else if (option instanceof OptionBlockSize) {
// TODO: figure out how to plumb in block size.
} else if (option instanceof StandardOpenOption) {
switch((StandardOpenOption) option) {
case CREATE:
case TRUNCATE_EXISTING:
case WRITE:
// Default behavior.
break;
case SPARSE:
// Ignored by specification.
break;
case CREATE_NEW:
writeOptions.add(Storage.BlobWriteOption.doesNotExist());
break;
case READ:
throw new IllegalArgumentException("READ+WRITE not supported yet");
case APPEND:
case DELETE_ON_CLOSE:
case DSYNC:
case SYNC:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof CloudStorageOption) {
// XXX: We need to interpret these later
} else {
throw new UnsupportedOperationException(option.toString());
}
}
if (!metas.isEmpty()) {
infoBuilder.setMetadata(metas);
}
if (!acls.isEmpty()) {
infoBuilder.setAcl(acls);
}
try {
return new CloudStorageWriteChannel(storage.writer(infoBuilder.build(), writeOptions.toArray(new Storage.BlobWriteOption[writeOptions.size()])));
} catch (StorageException oops) {
throw asIoException(oops);
}
}
Aggregations