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();
}
}
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;
}
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();
}
}
}
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();
}
Aggregations