Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -16,6 +16,7 @@

package com.google.cloud.storage;

import com.google.cloud.NoCredentials;
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.ServiceDefaults;
import com.google.cloud.ServiceOptions;
Expand Down Expand Up @@ -128,6 +129,13 @@ public static StorageOptions getDefaultInstance() {
return newBuilder().build();
}

/**
* Returns a unauthenticated {@code StorageOptions} instance.
*/
public static StorageOptions getUnauthenticated() {

This comment was marked as spam.

return newBuilder().setCredentials(NoCredentials.getInstance()).build();
}

@SuppressWarnings("unchecked")
@Override
public Builder toBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageRoles;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -1317,6 +1318,55 @@ public void testGetBlobs() {
assertTrue(remoteBlobs.get(1).delete());
}

@Test
public void testDownloadPublicBlobWithoutAuthentication() {
// create an unauthorized user
Storage unauthorizedStorage = StorageOptions.getUnauthenticated().getService();

// try to download blobs from a public bucket
String landsatBucket = "gcp-public-data-landsat";
String landsatPrefix = "LC08/PRE/044/034/LC80440342016259LGN00/";
String landsatBlob = landsatPrefix + "LC80440342016259LGN00_MTL.txt";
byte[] bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob);
assertEquals(7903, bytes.length);
int numBlobs = 0;
Iterator<Blob> blobIterator = unauthorizedStorage
.list(landsatBucket, Storage.BlobListOption.prefix(landsatPrefix))
.iterateAll().iterator();
while (blobIterator.hasNext()) {
numBlobs++;
blobIterator.next();
}
assertEquals(13, numBlobs);

This comment was marked as spam.

This comment was marked as spam.


// try to download blobs from a bucket that requires authentication
// authenticated client will succeed
// unauthenticated client will receive an exception
String sourceBlobName = "source-blob-name";
BlobInfo sourceBlob = BlobInfo.newBuilder(BUCKET, sourceBlobName).build();
assertNotNull(storage.create(sourceBlob));
assertNotNull(storage.readAllBytes(BUCKET, sourceBlobName));
try {
unauthorizedStorage.readAllBytes(BUCKET, sourceBlobName);
fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}

This comment was marked as spam.

assertTrue(storage.get(sourceBlob.getBlobId()).delete());

// try to upload blobs to a bucket that requires authentication
// authenticated client will succeed
// unauthenticated client will receive an exception
assertNotNull(storage.create(sourceBlob));
try {
unauthorizedStorage.create(sourceBlob);

This comment was marked as spam.

fail("Expected StorageException");
} catch (StorageException ex) {
// expected
}
assertTrue(storage.get(sourceBlob.getBlobId()).delete());
}

@Test
public void testGetBlobsFail() {
String sourceBlobName1 = "test-get-blobs-fail-1";
Expand Down