use of com.google.appengine.tools.cloudstorage.GcsFileMetadata 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.GcsFileMetadata in project iosched by google.
the class CloudFileManager method readFileAsJsonObject.
public JsonObject readFileAsJsonObject(GcsFilename file) throws IOException {
GcsFileMetadata metadata = gcsService.getMetadata(file);
if (metadata == null) {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development) {
// In the development server, try to fetch files on cloud storage via HTTP
Logger.getAnonymousLogger().info("fetching " + file.getObjectName() + " at " + Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
return RemoteJsonHelper.fetchJsonFromPublicURL(Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
}
return null;
}
GcsInputChannel readChannel = null;
try {
readChannel = gcsService.openReadChannel(file, 0);
JsonElement element = new JsonParser().parse(Channels.newReader(readChannel, DEFAULT_CHARSET_NAME));
return element.getAsJsonObject();
} finally {
if (readChannel != null) {
readChannel.close();
}
}
}
use of com.google.appengine.tools.cloudstorage.GcsFileMetadata in project activityinfo by bedatadriven.
the class GcsBlobServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
BlobId blobId = new BlobId(request.getParameter("blobId"));
ResourceId resourceId = ResourceId.valueOf(request.getParameter("resourceId"));
Preconditions.checkState(!Strings.isNullOrEmpty(blobId.asString()));
Preconditions.checkState(!Strings.isNullOrEmpty(resourceId.asString()));
AuthenticatedUser user = authProvider.get();
service.assertNotAnonymousUser(user);
service.assertHasAccess(user, blobId, resourceId);
service.assertBlobExists(blobId);
GcsFileMetadata metadata = GcsServiceFactory.createGcsService().getMetadata(new GcsFilename(service.getBucketName(), blobId.asString()));
response.setHeader("Content-Disposition", metadata.getOptions().getContentDisposition());
response.setContentType(metadata.getOptions().getMimeType());
BlobstoreServiceFactory.getBlobstoreService().serve(service.blobKey(blobId), response);
} catch (WebApplicationException e) {
sendError(response, e);
}
}
Aggregations