Advertisement
Guest User

Untitled

a guest
May 30th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. private static void uploadObject(boolean useCustomMetadata)
  2.                         throws IOException {
  3.                 View.header1("Uploading object.");
  4.                 final long objectSize = 100; /* 100 B */
  5.  
  6.                 // InputStreamContent mediaContent = new
  7.                 // InputStreamContent("application/octet-stream",
  8.                 // new RandomDataBlockInputStream(objectSize, 1024));
  9.                 FileInputStream file = new FileInputStream(new File(
  10.                                 "src/main/resources/image.jpg"));
  11.                 InputStreamContent mediaContent = new InputStreamContent("image/jpeg",
  12.                                 file);
  13.  
  14.                 // Not strictly necessary, but allows optimization in the cloud.
  15.                 // mediaContent.setLength(OBJECT_SIZE);
  16.  
  17.                 StorageObject objectMetadata = null;
  18.  
  19.                 if (useCustomMetadata) {
  20.                         // If you have custom settings for metadata on the object you want
  21.                         // to set
  22.                         // then you can allocate a StorageObject and set the values here.
  23.                         // You can
  24.                         // leave out setBucket(), since the bucket is in the insert
  25.                         // command's
  26.                         // parameters.
  27.                         List<ObjectAccessControl> acl = Lists.newArrayList();
  28.                         if (settings.getEmail() != null && !settings.getEmail().isEmpty()) {
  29.                                 acl.add(new ObjectAccessControl().setEntity(
  30.                                                 "user-" + settings.getEmail()).setRole("OWNER"));
  31.                         }
  32.                         if (settings.getDomain() != null && !settings.getDomain().isEmpty()) {
  33.                                 acl.add(new ObjectAccessControl().setEntity(
  34.                                                 "domain-" + settings.getDomain()).setRole("READER"));
  35.                         }
  36.                         objectMetadata = new StorageObject()
  37.                                         .setName(settings.getPrefix() + "myobjectX")
  38.                                         .setMetadata(
  39.                                                         ImmutableMap.of("key1", "value1", "key2", "value2"))
  40.                                         .setAcl(acl).setContentDisposition("attachment");
  41.                 }
  42.  
  43.                 Storage.Objects.Insert insertObject = storage.objects().insert(
  44.                                 settings.getBucket(), objectMetadata, mediaContent);
  45.  
  46.                 if (!useCustomMetadata) {
  47.                         // If you don't provide metadata, you will have specify the object
  48.                         // name by parameter. You will probably also want to ensure that
  49.                         // your
  50.                         // default object ACLs (a bucket property) are set appropriately:
  51.                         // https://developers.google.com/storage/docs/json_api/v1/buckets#defaultObjectAcl
  52.                         insertObject.setName(settings.getPrefix() + "myobjectX");
  53.                 }
  54.  
  55.                 insertObject.getMediaHttpUploader()
  56.                                 .setProgressListener(new CustomUploadProgressListener())
  57.                                 .setDisableGZipContent(true);
  58.                 // For small files, you may wish to call setDirectUploadEnabled(true),
  59.                 // to
  60.                 // reduce the number of HTTP requests made to the server.
  61.                 if (mediaContent.getLength() > 0
  62.                                 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
  63.                         insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
  64.                 }
  65.                 insertObject.execute();
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement