Search in sources :

Example 1 with GcsFilename

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

the class BlobAccessorGcsImpl method getBlob.

@Override
public BlobEntry getBlob(String fileName) throws UnexpectedServerException {
    GcsFilename gcsFileName = new GcsFilename(bucketName, fileName);
    try {
        GcsFileMetadata gcsFileMetadata = gcsService.getMetadata(gcsFileName);
        if (gcsFileMetadata == null)
            return null;
        if (gcsFileMetadata.getLength() == 0)
            return null;
        GcsInputChannel gcsInputChannel = gcsService.openReadChannel(gcsFileName, 0);
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) gcsFileMetadata.getLength());
        gcsInputChannel.read(byteBuffer);
        if (byteBuffer.position() != gcsFileMetadata.getLength()) {
            logger.log(Level.SEVERE, "Byte buffer size of " + byteBuffer.position() + " is not same as content lenght of " + gcsFileMetadata.getLength());
            throw new UnexpectedServerException();
        }
        return new BlobEntryGcsImpl(byteBuffer, gcsFileMetadata);
    } catch (IOException ex) {
        logger.log(Level.INFO, "Failed to fetch blob with name '" + fileName + "'", ex);
        throw new UnexpectedServerException();
    }
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) BlobEntryGcsImpl(com.pratilipi.data.type.gcs.BlobEntryGcsImpl) IOException(java.io.IOException) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) ByteBuffer(java.nio.ByteBuffer) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename)

Example 2 with GcsFilename

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

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

Example 4 with GcsFilename

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

the class PratilipiBackupApi method post.

@Post
public GenericResponse post(PostRequest request) throws UnexpectedServerException {
    Pratilipi pratilipi = DataAccessorFactory.getDataAccessor().getPratilipi(request.pratilipiId);
    Date dateTime = new Date(pratilipi.getLastUpdated().getTime() + TimeUnit.HOURS.toMillis(1L) - 1);
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd-HH'.00'-z");
    dateTimeFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
    GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    String srcBucket = "static.pratilipi.com";
    String dstBucket = "backup.pratilipi.com";
    String srcPrefix = "pratilipi/" + request.pratilipiId + "/";
    String dstPrefix = srcBucket + "/pratilipi-" + dateTimeFormat.format(dateTime) + "/" + request.pratilipiId + "/";
    try {
        ListResult result = gcsService.list(srcBucket, new ListOptions.Builder().setPrefix(srcPrefix).build());
        while (result.hasNext()) {
            String srcName = result.next().getName();
            String dstName = dstPrefix + srcName.substring(srcPrefix.length());
            gcsService.copy(new GcsFilename(srcBucket, srcName), new GcsFilename(dstBucket, dstName));
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "", e);
        throw new UnexpectedServerException();
    }
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) GcsService(com.google.appengine.tools.cloudstorage.GcsService) ListOptions(com.google.appengine.tools.cloudstorage.ListOptions) IOException(java.io.IOException) Date(java.util.Date) ListResult(com.google.appengine.tools.cloudstorage.ListResult) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Pratilipi(com.pratilipi.data.type.Pratilipi) SimpleDateFormat(java.text.SimpleDateFormat) GcsFilename(com.google.appengine.tools.cloudstorage.GcsFilename) Post(com.pratilipi.api.annotation.Post)

Aggregations

GcsFilename (com.google.appengine.tools.cloudstorage.GcsFilename)4 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)3 IOException (java.io.IOException)3 GcsFileOptions (com.google.appengine.tools.cloudstorage.GcsFileOptions)2 GcsOutputChannel (com.google.appengine.tools.cloudstorage.GcsOutputChannel)2 GcsFileMetadata (com.google.appengine.tools.cloudstorage.GcsFileMetadata)1 Builder (com.google.appengine.tools.cloudstorage.GcsFileOptions.Builder)1 GcsInputChannel (com.google.appengine.tools.cloudstorage.GcsInputChannel)1 GcsService (com.google.appengine.tools.cloudstorage.GcsService)1 ListOptions (com.google.appengine.tools.cloudstorage.ListOptions)1 ListResult (com.google.appengine.tools.cloudstorage.ListResult)1 Gson (com.google.gson.Gson)1 Post (com.pratilipi.api.annotation.Post)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 Pratilipi (com.pratilipi.data.type.Pratilipi)1 BlobEntryGcsImpl (com.pratilipi.data.type.gcs.BlobEntryGcsImpl)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ByteBuffer (java.nio.ByteBuffer)1 DateFormat (java.text.DateFormat)1