Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file in the google cloud storage using java

I have been trying for a long time to upload a file in the Google cloud store using java. By goggling I have found this code, but cant able to understand exactly. Can anyone please customize this one to upload a file in the GCS?

// Given
InputStream inputStream;  // object data, e.g., FileInputStream
long byteCount;  // size of input stream

InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", inputStream);
// Knowing the stream length allows server-side optimization, and client-side progress
// reporting with a MediaHttpUploaderProgressListener.
mediaContent.setLength(byteCount);

StorageObject objectMetadata = null;

if (useCustomMetadata) {
  // If you have custom settings for metadata on the object you want to set
  // then you can allocate a StorageObject and set the values here. You can
  // leave out setBucket(), since the bucket is in the insert command's
  // parameters.
  objectMetadata = new StorageObject()
      .setName("myobject")
      .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
      .setAcl(ImmutableList.of(
          new ObjectAccessControl().setEntity("domain-example.com").setRole("READER"),
          new ObjectAccessControl().setEntity("[email protected]").setRole("OWNER")
          ))
      .setContentDisposition("attachment");
}

Storage.Objects.Insert insertObject = storage.objects().insert("mybucket", objectMetadata,
    mediaContent);

if (!useCustomMetadata) {
  // If you don't provide metadata, you will have specify the object
  // name by parameter. You will probably also want to ensure that your
  // default object ACLs (a bucket property) are set appropriately:
  // https://developers.google.com/storage/docs/json_api/v1/buckets#defaultObjectAcl
  insertObject.setName("myobject");
}

// For small files, you may wish to call setDirectUploadEnabled(true), to
// reduce the number of HTTP requests made to the server.
if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
  insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
}

insertObject.execute();
like image 281
Sreekanth Avatar asked May 22 '14 13:05

Sreekanth


1 Answers

The recommended way to use Google Cloud Storage from Java is to use the Cloud Storage Client Libraries.

The GitHub page for this client gives several examples and resources to learn how to use it properly.

It also gives this code sample as an example of how to upload objects to Google Cloud Storage using the client library:

import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;

// Create your service object
Storage storage = StorageOptions.getDefaultInstance().getService();

// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
Bucket bucket = storage.create(BucketInfo.of(bucketName));

// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, "a simple blob".getBytes(UTF_8));
like image 86
Yannick MG Avatar answered Oct 28 '22 01:10

Yannick MG