Java Code Examples for org.jclouds.blobstore.BlobStore#putBlob()

The following examples show how to use org.jclouds.blobstore.BlobStore#putBlob() . 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: ObjectStoreFileStorageTest.java    From multiapps-controller with Apache License 2.0 6 votes vote down vote up
private String addBlobWithNoMetadata() throws Exception {
    BlobStore blobStore = blobStoreContext.getBlobStore();
    Path path = Paths.get(TEST_FILE_LOCATION);
    long fileSize = FileUtils.sizeOf(path.toFile());
    String id = UUID.randomUUID()
                    .toString();
    Blob blob = blobStore.blobBuilder(id)
                         .payload(new FileInputStream(path.toFile()))
                         .contentDisposition(path.getFileName()
                                                 .toString())
                         .contentType(MediaType.OCTET_STREAM.toString())
                         .contentLength(fileSize)
                         .build();
    blobStore.putBlob(CONTAINER, blob);
    return id;
}
 
Example 2
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 3
Source File: BlobStoreFileSystemHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
    public long saveInputStream(String id, String root, String filePath, InputStream stream) throws IOException {
        if(stream == null){
            return 0L;
        }
        ContainerAndName can = getContainerAndName(id, root, filePath);
        createContainerIfNotExist(can.container);

        InputStream in = markableInputStream(stream);
        long size = markableStreamLength(in);

        Payload payload = Payloads.newInputStreamPayload(in);

        try {
            BlobStore store = getBlobStore();
            String asciiID = Base64.encodeBase64String(id.getBytes("UTF8"));

            Blob blob = store.blobBuilder(can.name)
                .payload(payload)
                .contentLength(size)
                .userMetadata(ImmutableMap.of("id", asciiID, "path", filePath))
                .build();
            store.putBlob(can.container, blob);
        } finally {
            payload.release();
            Closeables.close(stream, true);
            Closeables.close(in, true);
        }

        return size;
    }
 
Example 4
Source File: JcloudsExpect100ContinueTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private void put(String name, String content) {
    BlobStore blobStore = context.getBlobStore();
    byte[] bytes = content.getBytes(Charsets.UTF_8);
    Blob blob = blobStore.blobBuilder(name)
            .payload(ByteSource.wrap(bytes))
            .contentLength(bytes.length)
            .build();
    try {
        blobStore.putBlob(containerName, blob);
    } catch (Exception e) {
        LOG.error("PUT " + name + " failed", e);
    }
}
 
Example 5
Source File: BlobStoreFileSystemHandler.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
    public long saveInputStream(String id, String root, String filePath, InputStream stream) throws IOException {
        if(stream == null){
            return 0L;
        }
        ContainerAndName can = getContainerAndName(id, root, filePath);
        createContainerIfNotExist(can.container);

        InputStream in = markableInputStream(stream);
        long size = markableStreamLength(in);

        Payload payload = Payloads.newInputStreamPayload(in);

        try {
            BlobStore store = getBlobStore();
            String asciiID = Base64.encodeBase64String(id.getBytes("UTF8"));

            Blob blob = store.blobBuilder(can.name)
                .payload(payload)
                .contentLength(size)
                .userMetadata(ImmutableMap.of("id", asciiID, "path", filePath))
                .build();
            store.putBlob(can.container, blob);
        } finally {
            payload.release();
            Closeables.close(stream, true);
            Closeables.close(in, true);
        }

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

   // Create a blob
   ByteSource payload = ByteSource.wrap("data".getBytes(Charsets.UTF_8));
   Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
         .payload(payload)
         .contentLength(payload.size())
         .build();

   // Put the blob in the container
   String blobId = blobstore.putBlob(containerName, blob);

   // Retrieve the blob
   Blob result = blobstore.getBlob(containerName, blobId);

   // Print the result
   InputStream is = result.getPayload().openStream();
   try {
      String data = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
      System.out.println("The retrieved payload is: " + data);
   } finally {
      is.close();
   }
}
 
Example 7
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);
    }
}