コード例 #1
0
  private void copyToS3(String fileName) {

    String bucketName = (String) properties.get(BUCKET_PROPNAME);
    String accessId = (String) properties.get(ACCESS_ID_PROPNAME);
    String secretKey = (String) properties.get(SECRET_KEY_PROPNAME);

    Properties overrides = new Properties();
    overrides.setProperty("s3" + ".identity", accessId);
    overrides.setProperty("s3" + ".credential", secretKey);

    final Iterable<? extends Module> MODULES =
        ImmutableSet.of(
            new JavaUrlHttpCommandExecutorServiceModule(),
            new Log4JLoggingModule(),
            new NettyPayloadModule());

    BlobStoreContext context =
        ContextBuilder.newBuilder("s3")
            .credentials(accessId, secretKey)
            .modules(MODULES)
            .overrides(overrides)
            .buildView(BlobStoreContext.class);

    // Create Container (the bucket in s3)
    try {
      AsyncBlobStore blobStore = context.getAsyncBlobStore(); // it can be changed to sync
      // BlobStore (returns false if it already exists)
      ListenableFuture<Boolean> container = blobStore.createContainerInLocation(null, bucketName);
      if (container.get()) {
        LOG.info("Created bucket " + bucketName);
      }
    } catch (Exception ex) {
      logger.error("Could not start binary service: {}", ex.getMessage());
      throw new RuntimeException(ex);
    }

    try {
      File file = new File(fileName);
      AsyncBlobStore blobStore = context.getAsyncBlobStore();
      BlobBuilder blobBuilder =
          blobStore
              .blobBuilder(file.getName())
              .payload(file)
              .calculateMD5()
              .contentType("text/plain")
              .contentLength(file.length());

      Blob blob = blobBuilder.build();

      ListenableFuture<String> futureETag =
          blobStore.putBlob(bucketName, blob, PutOptions.Builder.multipart());

      LOG.info("Uploaded file etag=" + futureETag.get());
    } catch (Exception e) {
      LOG.error("Error uploading to blob store", e);
    }
  }
コード例 #2
0
  public void testMultipartAsynchronouslySmallBlob()
      throws IOException, InterruptedException, Exception {
    String containerName = getContainerName();

    try {
      AsyncBlobStore asyncBlobStore = view.getAsyncBlobStore();
      asyncBlobStore.createContainerInLocation(null, containerName).get();
      Blob blob = asyncBlobStore.blobBuilder("small").payload("small").build();
      asyncBlobStore.putBlob(containerName, blob, PutOptions.Builder.multipart()).get();

    } finally {
      returnContainer(containerName);
    }
  }