Search in sources :

Example 1 with Builder

use of com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder in project pratilipi by Pratilipi.

the class BlobAccessorGcsImpl method createOrUpdateBlob.

@Override
public BlobEntry createOrUpdateBlob(BlobEntry blobEntry) throws UnexpectedServerException {
    GcsFilename gcsFileName = new GcsFilename(bucketName, blobEntry.getName());
    Builder builder = new GcsFileOptions.Builder();
    if (blobEntry.getMimeType() != null)
        builder.mimeType(blobEntry.getMimeType());
    if (blobEntry.getCacheControl() != null)
        builder.cacheControl(blobEntry.getCacheControl());
    if (blobEntry.getMetaName() != null)
        builder.addUserMetadata(BlobEntry.META_NAME, blobEntry.getMetaName());
    GcsFileOptions gcsFileOptions = builder.build();
    try {
        GcsOutputChannel gcsOutputChannel = gcsService.createOrReplace(gcsFileName, gcsFileOptions);
        gcsOutputChannel.write(ByteBuffer.wrap(blobEntry.getData()));
        gcsOutputChannel.close();
    } catch (IOException ex) {
        logger.log(Level.INFO, "Failed to create/update blob with name '" + blobEntry.getName() + "'", ex);
        throw new UnexpectedServerException();
    }
    return null;
}
Also used : GcsFileOptions(com.google.appengine.tools.cloudstorage.GcsFileOptions) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Builder(com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder) IOException(java.io.IOException) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) GcsOutputChannel(com.google.appengine.tools.cloudstorage.GcsOutputChannel)

Example 2 with Builder

use of com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder in project activityinfo by bedatadriven.

the class GcsBlobFieldStorageService method put.

public void put(AuthenticatedUser user, String contentDisposition, String mimeType, BlobId blobId, ResourceId formId, InputStream inputStream) throws IOException {
    ResourceId userId = CuidAdapter.userId(user.getUserId());
    assertNotAnonymousUser(user);
    if (!hasAccessToResource(userId, formId)) {
        throw new WebApplicationException(UNAUTHORIZED);
    }
    GcsFileOptions gcsFileOptions = new Builder().contentDisposition(contentDisposition).mimeType(mimeType).addUserMetadata(GcsUploadCredentialBuilder.X_CREATOR, userId.asString()).addUserMetadata(GcsUploadCredentialBuilder.X_OWNER, formId.asString()).build();
    GcsOutputChannel channel = GcsServiceFactory.createGcsService().createOrReplace(new GcsFilename(bucketName, blobId.asString()), gcsFileOptions);
    try (OutputStream outputStream = Channels.newOutputStream(channel)) {
        IOUtils.copy(inputStream, outputStream);
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) Builder(com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder) OutputStream(java.io.OutputStream)

Example 3 with Builder

use of com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder in project activityinfo by bedatadriven.

the class SubmissionArchiver method backup.

public void backup(ResourceId formClassId, ResourceId formId, ByteSource byteSource) {
    try {
        DateTime submissionTime = new DateTime(DateTimeZone.UTC);
        String path = Joiner.on('/').join(formClassId, dateFormat.print(submissionTime), timeFormat.print(submissionTime) + " " + formId + ".xml");
        GcsFilename gcsFilename = new GcsFilename(bucketName, path);
        GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
        GcsFileOptions gcsFileOptions = new Builder().mimeType(MediaType.MULTIPART_FORM_DATA).build();
        GcsOutputChannel channel = gcsService.createOrReplace(gcsFilename, gcsFileOptions);
        try (OutputStream outputStream = Channels.newOutputStream(channel)) {
            byteSource.copyTo(outputStream);
        }
        LOGGER.log(Level.INFO, "Archived XForm to " + gcsFilename);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Form submission could not be backed up to GCS", e);
        try {
            LOGGER.info(byteSource.asCharSource(Charsets.UTF_8).read());
        } catch (IOException ioException) {
        }
    }
}
Also used : Builder(com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder) OutputStream(java.io.OutputStream) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException)

Aggregations

Builder (com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 GcsFileOptions (com.google.appengine.tools.cloudstorage.GcsFileOptions)1 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)1 GcsOutputChannel (com.google.appengine.tools.cloudstorage.GcsOutputChannel)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 DateTime (org.joda.time.DateTime)1