Example #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);
    }
  }
  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);
    }
  }
Example #3
0
  public void testCreateParentIfNeededAsyncNestedPath() {
    AsyncBlobStore asyncBlobStore = createMock(AsyncBlobStore.class);
    String container = "container";
    Blob blob = createMock(Blob.class);
    MutableBlobMetadata md = createMock(MutableBlobMetadata.class);

    expect(blob.getMetadata()).andReturn(md).atLeastOnce();
    expect(md.getName()).andReturn("rootpath/subpath/hello").atLeastOnce();
    expect(asyncBlobStore.createDirectory("container", "rootpath/subpath")).andReturn(null);

    replay(asyncBlobStore);
    replay(blob);
    replay(md);

    createParentIfNeededAsync(asyncBlobStore, container, blob);

    verify(asyncBlobStore);
    verify(blob);
    verify(md);
  }
  public static ListenableFuture<Void> createParentIfNeededAsync(
      AsyncBlobStore asyncBlobStore, String container, Blob blob) {
    checkNotNull(asyncBlobStore, "asyncBlobStore");
    checkNotNull(container, "container");

    String name = blobName.apply(blob);
    if (name.indexOf('/') > 0) {
      return asyncBlobStore.createDirectory(container, parseDirectoryFromPath(name));
    } else {
      return Futures.immediateFuture(null);
    }
  }