/** http://code.google.com/p/jclouds/issues/detail?id=992 */
  public void testUseBucketWithUpperCaseName() throws Exception {
    String bucketName = CONTAINER_PREFIX + "-TestBucket";
    String blobName = "TestBlob.txt";
    StorageMetadata container = null;
    BlobStore store = view.getBlobStore();

    // Create and use a valid bucket name with uppercase characters in the bucket name (US regions
    // only)
    try {
      store.createContainerInLocation(null, bucketName);

      for (StorageMetadata metadata : store.list()) {
        if (metadata.getName().equals(bucketName)) {
          container = metadata;
          break;
        }
      }

      assertNotNull(container);

      store.putBlob(
          bucketName,
          store.blobBuilder(blobName).payload("This is a test!").contentType("text/plain").build());

      assertNotNull(store.getBlob(bucketName, blobName));
    } finally {
      if (container != null) {
        store.deleteContainer(bucketName);
      }
    }

    // Try to create the same bucket successfully created above in one of the non-US regions to
    // ensure an error is
    // encountered as expected.
    Location location = null;

    for (Location pLocation : store.listAssignableLocations()) {
      if (!ImmutableSet.of(Region.US_STANDARD, Region.US_EAST_1, Region.US_WEST_1, Region.US_WEST_2)
          .contains(pLocation.getId())) {
        location = pLocation;
        break;
      }
    }

    try {
      store.createContainerInLocation(location, bucketName);
      fail("Should had failed because in non-US regions, mixed-case bucket names are invalid.");
    } catch (AWSResponseException e) {
      assertEquals("InvalidBucketName", e.getError().getCode());
    }
  }
  @Test
  void testToBlob() {
    assertNull(ToBlob.INSTANCE.apply(null));
    BlobStore blobStore = getBlobStore();
    blobStore.createContainerInLocation(null, "container");
    blobStore.createDirectory("container", "one");

    blobStore.putBlob(
        "container",
        blobStore.blobBuilder("myblob").payload(ByteSource.wrap("testcontent".getBytes())).build());
    Blob representation = ToBlob.INSTANCE.apply(blobStore.getBlob("container", "myblob"));
    assertNotNull(representation);
    assertNotNull(representation.getBlobMetadata());
  }
  /**
   * Upload a large object from a File using the BlobStore API.
   *
   * @throws ExecutionException
   * @throws InterruptedException
   */
  private void uploadLargeObjectFromFile(File largeFile)
      throws InterruptedException, ExecutionException {
    System.out.format("Upload Large Object From File%n");

    ByteSource source = Files.asByteSource(largeFile);
    // create the payload and set the content length
    Payload payload = Payloads.newByteSourcePayload(source);
    payload.getContentMetadata().setContentLength(largeFile.length());

    Blob blob = blobStore.blobBuilder(largeFile.getName()).payload(payload).build();

    // configure the blobstore to use multipart uploading of the file
    String eTag = blobStore.putBlob(CONTAINER, blob, multipart());

    System.out.format("  Uploaded %s eTag=%s", largeFile.getName(), eTag);
  }
  public void testMultipartChunkedFileStream() throws IOException, InterruptedException {

    File file = new File("target/const.txt");
    Files.copy(oneHundredOneConstitutions, file);
    String containerName = getContainerName();

    try {
      BlobStore blobStore = view.getBlobStore();
      blobStore.createContainerInLocation(null, containerName);
      Blob blob = blobStore.blobBuilder("const.txt").payload(file).build();
      blobStore.putBlob(containerName, blob, PutOptions.Builder.multipart());

    } finally {
      returnContainer(containerName);
    }
  }
  /** Test that BlobRequestSigner creates expected URIs. */
  public void testBlobRequestSigner() throws Exception {
    String containerName = "container";
    String blobName = "blob";
    URI endPoint =
        new URI(
            "http",
            "localhost",
            String.format("/transient/%s/%s", containerName, blobName),
            /*fragment=*/ null);
    BlobRequestSigner signer = context.getSigner();
    HttpRequest request;
    HttpRequest expected;

    request = signer.signGetBlob(containerName, blobName);
    expected =
        HttpRequest.builder()
            .method("GET")
            .endpoint(endPoint)
            .headers(request.getHeaders())
            .build();
    assertEquals(expected, request);

    request = signer.signRemoveBlob(containerName, blobName);
    expected =
        HttpRequest.builder()
            .method("DELETE")
            .endpoint(endPoint)
            .headers(request.getHeaders())
            .build();
    assertEquals(expected, request);

    Blob blob = blobStore.blobBuilder(blobName).forSigning().build();
    request = signer.signPutBlob(containerName, blob);
    expected =
        HttpRequest.builder()
            .method("PUT")
            .endpoint(endPoint)
            .headers(request.getHeaders())
            .payload(new PhantomPayload())
            .build();
    assertEquals(expected, request);
  }
  public void testPutWithReducedRedundancyStorage() throws InterruptedException {
    String containerName = getContainerName();
    try {
      String blobName = "test-rrs";
      BlobStore blobStore = view.getBlobStore();
      blobStore.createContainerInLocation(null, containerName);

      Blob blob = blobStore.blobBuilder(blobName).payload("something").build();
      blobStore.putBlob(containerName, blob, storageClass(StorageClass.REDUCED_REDUNDANCY));

      S3Client s3Client = S3Client.class.cast(view.unwrap(AWSS3ApiMetadata.CONTEXT_TOKEN).getApi());
      ListBucketResponse response = s3Client.listBucket(containerName, withPrefix(blobName));

      ObjectMetadata metadata = response.iterator().next();
      assertEquals(metadata.getStorageClass(), StorageClass.REDUCED_REDUNDANCY);

    } finally {
      returnContainer(containerName);
    }
  }
  public void testRanges() throws IOException {
    blobStore.createContainerInLocation(null, CONTAINER_NAME);
    String input = "abcdefgh";
    Payload payload;
    Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(input)).build();
    blobStore.putBlob(CONTAINER_NAME, blob);

    GetOptions getOptionsRangeStartAt = new GetOptions();
    getOptionsRangeStartAt.startAt(1);
    Blob blobRangeStartAt =
        blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt);
    payload = blobRangeStartAt.getPayload();
    try {
      assertEquals(input.substring(1), Strings2.toString(payload));
    } finally {
      Closeables.closeQuietly(payload);
    }

    GetOptions getOptionsRangeTail = new GetOptions();
    getOptionsRangeTail.tail(3);
    Blob blobRangeTail =
        blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail);
    payload = blobRangeTail.getPayload();
    try {
      assertEquals(input.substring(5), Strings2.toString(payload));
    } finally {
      Closeables.closeQuietly(payload);
    }

    GetOptions getOptionsFragment = new GetOptions();
    getOptionsFragment.range(4, 6);
    Blob blobFragment =
        blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment);
    payload = blobFragment.getPayload();
    try {
      assertEquals(input.substring(4, 7), Strings2.toString(payload));
    } finally {
      Closeables.closeQuietly(payload);
    }
  }
 /**
  * Creates a {@link Blob} object filled with data from a file
  *
  * @param keyName
  * @param fileContent
  * @return
  */
 private Blob createBlob(String keyName, File filePayload) {
   return blobStore.blobBuilder(keyName).payload(filePayload).build();
 }
 private void uploadSinglePart(BlobStore blobStore, String container, String storagePath) {
   Blob blob = blobStore.blobBuilder(storagePath).payload(createPayload()).build();
   blobStore.putBlob(container, blob);
 }