org.jclouds.blobstore.options.PutOptions Java Examples

The following examples show how to use org.jclouds.blobstore.options.PutOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EventualBlobStore.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Override
public String putBlob(final String containerName, Blob blob,
        final PutOptions options) {
    final String nearName = blob.getMetadata().getName();
    String nearETag = writeStore.putBlob(containerName, blob, options);
    schedule(new Callable<String>() {
            @Override
            public String call() {
                Blob nearBlob = writeStore.getBlob(containerName, nearName);
                String farETag = delegate().putBlob(containerName,
                        nearBlob, options);
                return farETag;
            }
        });
    return nearETag;
}
 
Example #2
Source File: S3ProxyHandler.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
private static void handleAbortMultipartUpload(HttpServletResponse response,
        BlobStore blobStore, String containerName, String blobName,
        String uploadId) throws IOException, S3Exception {
    if (Quirks.MULTIPART_REQUIRES_STUB.contains(getBlobStoreType(
            blobStore))) {
        if (!blobStore.blobExists(containerName, uploadId)) {
            throw new S3Exception(S3ErrorCode.NO_SUCH_UPLOAD);
        }

        blobStore.removeBlob(containerName, uploadId);
    }

    // TODO: how to reconstruct original mpu?
    MultipartUpload mpu = MultipartUpload.create(containerName,
            blobName, uploadId, createFakeBlobMetadata(blobStore),
            new PutOptions());
    blobStore.abortMultipartUpload(mpu);
    response.sendError(HttpServletResponse.SC_NO_CONTENT);
}
 
Example #3
Source File: NullBlobStore.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Override
public String putBlob(String containerName, Blob blob,
        PutOptions options) {
    long length;
    try (InputStream is = blob.getPayload().openStream()) {
        length = ByteStreams.copy(is, ByteStreams.nullOutputStream());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    byte[] array = Longs.toByteArray(length);
    ByteSourcePayload payload = new ByteSourcePayload(
            ByteSource.wrap(array));
    payload.setContentMetadata(blob.getPayload().getContentMetadata());
    payload.getContentMetadata().setContentLength((long) array.length);
    payload.getContentMetadata().setContentMD5((HashCode) null);
    blob.setPayload(payload);

    return super.putBlob(containerName, blob, options);
}
 
Example #4
Source File: MainApp.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
private static void multipartUploadExample(BlobStore blobstore) throws IOException {
   // Create a container
   String containerName = "jclouds_multipartUploadExample_" + UUID.randomUUID().toString();
   blobstore.createContainerInLocation(null, containerName); // Create a vault

   // Create a blob
   ByteSource payload = buildData(16 * MiB);
   Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
         .payload(payload)
         .contentLength(payload.size())
         .build();

   // Create the PutOptions
   PutOptions options = PutOptions.Builder.multipart(true);

   // Put the blob in the container
   blobstore.putBlob(containerName, blob, options);
   System.out.println("The blob has been uploaded");
}
 
Example #5
Source File: ObjectStoreFileStorage.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private void putBlobWithRetries(Blob blob, int retries) {
    for (int i = 1; i <= retries; i++) {
        try {
            blobStore.putBlob(container, blob, PutOptions.Builder.multipart());
            return;
        } catch (HttpResponseException e) {
            LOGGER.warn(MessageFormat.format(Messages.ATTEMPT_TO_UPLOAD_BLOB_FAILED, i, retries, e.getMessage()), e);
            if (i == retries) {
                throw e;
            }
        }
        MiscUtil.sleep(i * getRetryWaitTime());
    }
}
 
Example #6
Source File: EventualBlobStore.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Override
public MultipartUpload initiateMultipartUpload(String container,
        BlobMetadata blobMetadata, PutOptions options) {
    MultipartUpload mpu = delegate().initiateMultipartUpload(container,
            blobMetadata, options);
    return mpu;
}
 
Example #7
Source File: EventualBlobStoreTest.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadAfterMultipartUpload() throws Exception {
    String blobName = createRandomBlobName();
    Blob blob = makeBlob(eventualBlobStore, blobName);
    MultipartUpload mpu = eventualBlobStore.initiateMultipartUpload(
            containerName, blob.getMetadata(), new PutOptions());
    MultipartPart part = eventualBlobStore.uploadMultipartPart(mpu,
            /*partNumber=*/ 1, blob.getPayload());
    eventualBlobStore.completeMultipartUpload(mpu, ImmutableList.of(part));
    assertThat(eventualBlobStore.getBlob(containerName, blobName))
            .isNull();
    delay();
    validateBlob(eventualBlobStore.getBlob(containerName, blobName));
}
 
Example #8
Source File: ReadOnlyBlobStoreTest.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutBlobOptions() throws Exception {
    try {
        readOnlyBlobStore.putBlob(containerName, null, new PutOptions());
        Fail.failBecauseExceptionWasNotThrown(
                UnsupportedOperationException.class);
    } catch (UnsupportedOperationException ne) {
        // expected
    }
}
 
Example #9
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
@Override
public String putBlob(String container, Blob blob, PutOptions options) {
    return putBlob(container, blob);
}
 
Example #10
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
@Override
public MultipartUpload initiateMultipartUpload(String container, BlobMetadata blob, PutOptions options) {
    throw new UnsupportedOperationException();
}
 
Example #11
Source File: EventualBlobStore.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Override
public String putBlob(String containerName, Blob blob) {
    return putBlob(containerName, blob, PutOptions.NONE);
}
 
Example #12
Source File: S3ProxyHandler.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
private void handleInitiateMultipartUpload(HttpServletRequest request,
        HttpServletResponse response, BlobStore blobStore,
        String containerName, String blobName)
        throws IOException, S3Exception {
    ByteSource payload = ByteSource.empty();
    BlobBuilder.PayloadBlobBuilder builder = blobStore
            .blobBuilder(blobName)
            .payload(payload);
    addContentMetdataFromHttpRequest(builder, request);
    builder.contentLength(payload.size());

    String storageClass = request.getHeader(AwsHttpHeaders.STORAGE_CLASS);
    if (storageClass == null || storageClass.equalsIgnoreCase("STANDARD")) {
        // defaults to STANDARD
    } else {
        builder.tier(StorageClass.valueOf(storageClass).toTier());
    }

    BlobAccess access;
    String cannedAcl = request.getHeader(AwsHttpHeaders.ACL);
    if (cannedAcl == null || cannedAcl.equalsIgnoreCase("private")) {
        access = BlobAccess.PRIVATE;
    } else if (cannedAcl.equalsIgnoreCase("public-read")) {
        access = BlobAccess.PUBLIC_READ;
    } else if (CANNED_ACLS.contains(cannedAcl)) {
        throw new S3Exception(S3ErrorCode.NOT_IMPLEMENTED);
    } else {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    PutOptions options = new PutOptions().setBlobAccess(access);

    MultipartUpload mpu = blobStore.initiateMultipartUpload(containerName,
            builder.build().getMetadata(), options);

    if (Quirks.MULTIPART_REQUIRES_STUB.contains(getBlobStoreType(
            blobStore))) {
        blobStore.putBlob(containerName, builder.name(mpu.id()).build(),
                options);
    }

    response.setCharacterEncoding(UTF_8);
    try (Writer writer = response.getWriter()) {
        response.setContentType(XML_CONTENT_TYPE);
        XMLStreamWriter xml = xmlOutputFactory.createXMLStreamWriter(
                writer);
        xml.writeStartDocument();
        xml.writeStartElement("InitiateMultipartUploadResult");
        xml.writeDefaultNamespace(AWS_XMLNS);

        writeSimpleElement(xml, "Bucket", containerName);
        writeSimpleElement(xml, "Key", blobName);
        writeSimpleElement(xml, "UploadId", mpu.id());

        xml.writeEndElement();
        xml.flush();
    } catch (XMLStreamException xse) {
        throw new IOException(xse);
    }
}
 
Example #13
Source File: NullBlobStore.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Override
public String putBlob(String containerName, Blob blob) {
    return putBlob(containerName, blob, PutOptions.NONE);
}
 
Example #14
Source File: ReadOnlyBlobStore.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Override
public String putBlob(final String containerName, Blob blob,
        final PutOptions options) {
    throw new UnsupportedOperationException("read-only BlobStore");
}
 
Example #15
Source File: ReadOnlyBlobStore.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Override
public MultipartUpload initiateMultipartUpload(String container,
        BlobMetadata blobMetadata, PutOptions options) {
    throw new UnsupportedOperationException("read-only BlobStore");
}
 
Example #16
Source File: NullBlobStoreTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateMultipartBlobGetBlob() throws Exception {
    String blobName = "multipart-upload";
    BlobMetadata blobMetadata = makeBlob(nullBlobStore, blobName)
            .getMetadata();
    MultipartUpload mpu = nullBlobStore.initiateMultipartUpload(
            containerName, blobMetadata, new PutOptions());

    ByteSource byteSource = TestUtils.randomByteSource().slice(
            0, nullBlobStore.getMinimumMultipartPartSize() + 1);
    ByteSource byteSource1 = byteSource.slice(
            0, nullBlobStore.getMinimumMultipartPartSize());
    ByteSource byteSource2 = byteSource.slice(
            nullBlobStore.getMinimumMultipartPartSize(), 1);
    Payload payload1 = Payloads.newByteSourcePayload(byteSource1);
    Payload payload2 = Payloads.newByteSourcePayload(byteSource2);
    payload1.getContentMetadata().setContentLength(byteSource1.size());
    payload2.getContentMetadata().setContentLength(byteSource2.size());
    MultipartPart part1 = nullBlobStore.uploadMultipartPart(mpu, 1,
            payload1);
    MultipartPart part2 = nullBlobStore.uploadMultipartPart(mpu, 2,
            payload2);

    List<MultipartPart> parts = nullBlobStore.listMultipartUpload(mpu);
    assertThat(parts.get(0).partNumber()).isEqualTo(1);
    assertThat(parts.get(0).partSize()).isEqualTo(byteSource1.size());
    assertThat(parts.get(0).partETag()).isEqualTo(part1.partETag());
    assertThat(parts.get(1).partNumber()).isEqualTo(2);
    assertThat(parts.get(1).partSize()).isEqualTo(byteSource2.size());
    assertThat(parts.get(1).partETag()).isEqualTo(part2.partETag());

    assertThat(nullBlobStore.listMultipartUpload(mpu)).hasSize(2);

    nullBlobStore.completeMultipartUpload(mpu, parts);

    Blob newBlob = nullBlobStore.getBlob(containerName, blobName);
    validateBlobMetadata(newBlob.getMetadata());

    // content differs, only compare length
    try (InputStream actual = newBlob.getPayload().openStream();
            InputStream expected = byteSource.openStream()) {
        long actualLength = ByteStreams.copy(actual,
                ByteStreams.nullOutputStream());
        long expectedLength = ByteStreams.copy(expected,
                ByteStreams.nullOutputStream());
        assertThat(actualLength).isEqualTo(expectedLength);
    }

    nullBlobStore.removeBlob(containerName, blobName);
    assertThat(nullBlobStore.list(containerName)).isEmpty();
}