Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload a file to Google Cloud Storage (Java)

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) ?

like image 546
mangusta Avatar asked Jul 16 '18 09:07

mangusta


2 Answers

I was able to create and upload to a bucket using the com.google.cloud.storage libraries in the following snippet derived from the documentation:

public void createBlobFromByteArray(String blobName, String jsonPath, String bucketName) throws IOException {
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

  Bucket bucket = storage.create(BucketInfo.of(bucketName));

  Blob blob = bucket.create(blobName, "Hello, World!".getBytes(UTF_8), "text/plain");
}

You need to create a JSON service account key in and download it from the Google Cloud console. Then pass it in the GoogleCredentials method to authenticate 'storage'

like image 178
Yasser Karout Avatar answered Oct 16 '22 22:10

Yasser Karout


  • Export the Google credential JSON from command line:
export GOOGLE_APPLICATION_CREDENTIALS='\path\key.json'
  • Run this code in application:
Storage storage = StorageOptions.getDefaultInstance().getService();
BlobId blobId = BlobId.of(BUCKET_NAME, OBJECT_NAME);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, "Hello, World!".getBytes(UTF_8));
  
like image 38
Md. Shahariar Hossen Avatar answered Oct 16 '22 23:10

Md. Shahariar Hossen