Basically, the issue is in the title: I cannot figure out how to upload a file to Google Storage
I have checked this webpage:
https://cloud.google.com/storage/docs/uploading-objects
These two lines are suggested for "uploading objects" in Java language tab:
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
Blob blob = bucket.create(blobName, content, "text/plain");
The bucket
object above can be received from storage
object, and that's actually the root of the problem
In all tutorials I have seen so far (particularly in this one: https://support.google.com/dfp_premium/answer/1733127?hl=en), the storage
belongs to com.google.api.services.storage.Storage
instead of com.google.cloud.storage.Storage
, and is instantiated as follows:
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JacksonFactory.getDefaultInstance())
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(
Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(p12File).build();
storage = new com.google.api.services.storage.Storage.Builder(httpTransport,
JacksonFactory.getDefaultInstance(), credential)
.setApplicationName(PROJECT_NAME).build();
After creating a storage object in this way, we may obtain a bucket object as follows:
com.google.api.services.storage.Storage.Buckets.Get object = storage.buckets().get(BUCKET_NAME);
com.google.api.services.storage.model.Bucket bucket = object.execute();
But I can't use com.google.api.services.storage.model.Bucket
for uploading files because it doesn't have create()
method mentioned above. I need to use com.google.cloud.storage.Bucket
instead.
I have checked this stackoverflow question too: Upload image to Google Cloud Storage (Java)
and the examples in the provided links indeed use com.google.cloud.storage.Storage
and com.google.cloud.storage.Bucket
but they never show HOW to pass credentials and instantiate storage
object similar to the code above.
Could someone provide a clear and comprehensive Java snippet showing how to upload a file to Google Cloud Storage, including all necessary credential instantiations of storage
object?
EDIT. In other words, I need to customize the instantiation of com.google.cloud.storage.Storage
like above, instead of using this line: Storage storage = StorageOptions.getDefaultInstance().getService();
EDIT2. Finally I was able to upload the file after retrieving the bucket by the following code now:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(KEY_JSN)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/devstorage.read_write"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
Bucket bucket = storage.get(BUCKET_NAME);
One single subtle point remaining for me, is that the following line:
Page<Bucket> buckets = storage.list();
returns an empty list. Does it make sense to receive an empty bucket list while being able to receive a particular bucket meta by get(bucket_name)
?
storage.list()
should return you aPage<Bucket>
object, which you can iterate like this. Ignore the options in thelist()
method, it's just options to limits the results. – amportstorage.list().iterateAll()
then I'm gettingIterable<Bucket>
from it, then I'm gettingIterator<Bucket>
but it is empty. (the code in that link is incorrect,iterateAll()
returnsIterable
, notIterator
) – mangusta