Search in sources :

Example 1 with ListResult

use of com.google.appengine.tools.cloudstorage.ListResult 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)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 Post (com.pratilipi.api.annotation.Post)1 GenericResponse (com.pratilipi.api.shared.GenericResponse)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 Pratilipi (com.pratilipi.data.type.Pratilipi)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1