upload a file in the google cloud storage using java

0 views
Skip to first unread message

user3487948 via StackOverflow

unread,
May 22, 2014, 9:37:17 AM5/22/14
to google-appengin...@googlegroups.com

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 any one please customize this one to upload a file in the GCS. Any one can help me to come out of this.

// 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("user-admi...@example.com").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();


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/23808432/upload-a-file-in-the-google-cloud-storage-using-java
Reply all
Reply to author
Forward
0 new messages