Search in sources :

Example 1 with GcsFileOptions

use of com.google.appengine.tools.cloudstorage.GcsFileOptions 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 GcsFileOptions

use of com.google.appengine.tools.cloudstorage.GcsFileOptions in project iosched by google.

the class CloudFileManager method createOrUpdate.

/**
   * Create or update a file in a GCC bucket, using the default ACL for the bucket.
   *
   * @param filename Name of file to create
   * @param contents File contents
   * @param shortCache If true, sets cache expiry to 0 sec. Otherwise, cache expiry is set to 6,000 sec.
   * @throws IOException
   */
public void createOrUpdate(String filename, JsonElement contents, boolean shortCache) throws IOException {
    GcsFilename file = new GcsFilename(defaultBucket, filename);
    GcsFileOptions options = new GcsFileOptions.Builder().mimeType("application/json").cacheControl("public, max-age=" + (shortCache ? 0 : 6000)).build();
    GcsOutputChannel writeChannel = null;
    try {
        writeChannel = gcsService.createOrReplace(file, options);
        Writer writer = Channels.newWriter(writeChannel, DEFAULT_CHARSET_NAME);
        new Gson().toJson(contents, writer);
        writer.flush();
    } finally {
        if (writeChannel != null) {
            writeChannel.close();
        }
    }
}
Also used : GcsFileOptions(com.google.appengine.tools.cloudstorage.GcsFileOptions) Gson(com.google.gson.Gson) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) GcsOutputChannel(com.google.appengine.tools.cloudstorage.GcsOutputChannel) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

GcsFileOptions (com.google.appengine.tools.cloudstorage.GcsFileOptions)2 GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)2 GcsOutputChannel (com.google.appengine.tools.cloudstorage.GcsOutputChannel)2 Builder (com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder)1 Gson (com.google.gson.Gson)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1