本文整理汇总了Java中com.google.appengine.tools.cloudstorage.GcsInputChannel类的典型用法代码示例。如果您正苦于以下问题:Java GcsInputChannel类的具体用法?Java GcsInputChannel怎么用?Java GcsInputChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GcsInputChannel类属于com.google.appengine.tools.cloudstorage包,在下文中一共展示了GcsInputChannel类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFileAsJsonObject
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
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();
}
}
}
示例2: openTempFile
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
@Override
public InputStream openTempFile(String fileName) throws IOException {
if (!fileName.startsWith("__TEMP__")) {
throw new RuntimeException("deleteTempFile (" + fileName + ") Invalid File Name");
}
GcsFilename gcsFileName = new GcsFilename(GCS_BUCKET_NAME, fileName);
int fileSize = (int) gcsService.getMetadata(gcsFileName).getLength();
ByteBuffer resultBuffer = ByteBuffer.allocate(fileSize);
GcsInputChannel readChannel = gcsService.openReadChannel(gcsFileName, 0);
int bytesRead = 0;
try {
while (bytesRead < fileSize) {
bytesRead += readChannel.read(resultBuffer);
}
} finally {
readChannel.close();
}
return new ByteArrayInputStream(resultBuffer.array());
}
示例3: testReadGsObj
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
@Test
@InSequence(2)
public void testReadGsObj() throws IOException {
GcsFilename filename = new GcsFilename(bucket, OBJECT_NAME);
String objContent;
try (GcsInputChannel readChannel = gcsService.openReadChannel(filename, 0)) {
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
String line;
objContent = "";
while ((line = reader.readLine()) != null) {
objContent += line;
}
}
assertTrue(objContent.indexOf(CONTENT) == 0);
assertTrue(objContent.indexOf(MORE_WORDS) > 0);
}
示例4: readString
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
public static String readString(final String stringFileName) throws IOException {
GcsFilename fileName = open(stringFileName);
int fileSize = (int) gcsService.getMetadata(fileName).getLength();
ByteBuffer result = ByteBuffer.allocate(fileSize);
try (GcsInputChannel readChannel = gcsService.openReadChannel(fileName, 0)) {
readChannel.read(result);
String v = new String( result.array() , Charset.forName("UTF-8") );
return v;
}
}
示例5: doGet
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world from java");
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);
GcsFileOptions options = new GcsFileOptions.Builder()
.mimeType("text/html")
.acl("public-read")
.addUserMetadata("myfield1", "my field value")
.build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
PrintWriter writer = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
writer.println("The woods are lovely dark and deep.");
writer.println("But I have promises to keep.");
writer.flush();
writeChannel.waitForOutstandingWrites();
writeChannel.write(ByteBuffer.wrap("And miles to go before I sleep.".getBytes("UTF8")));
writeChannel.close();
resp.getWriter().println("Done writing...");
GcsInputChannel readChannel = null;
BufferedReader reader = null;
try {
readChannel = gcsService.openReadChannel(filename, 0);
reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
String line;
while ((line = reader.readLine()) != null) {
resp.getWriter().println("READ:" + line);
}
} finally {
if (reader != null) { reader.close(); }
}
}
开发者ID:GoogleCloudPlatform,项目名称:appengine-gcs-client,代码行数:40,代码来源:PortOfFilesAPIGuestbookServlet.java
示例6: doGet
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
/**
* Retrieves a file from GCS and returns it in the http response.
* If the request path is /gcs/Foo/Bar this will be interpreted as
* a request to read the GCS file named Bar in the bucket Foo.
*/
//[START doGet]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
GcsFilename fileName = getFileName(req);
if (SERVE_USING_BLOBSTORE_API) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey(
"/gs/" + fileName.getBucketName() + "/" + fileName.getObjectName());
blobstoreService.serve(blobKey, resp);
} else {
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(fileName, 0, BUFFER_SIZE);
copy(Channels.newInputStream(readChannel), resp.getOutputStream());
}
}
示例7: readFromCache
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
private ValidatedSignedDoc readFromCache(GcsFilename gcsFileName) throws IOException {
GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel(gcsFileName, 0, 1024 * 1024);
try (ObjectInputStream oin = new ObjectInputStream(Channels.newInputStream(readChannel))) {
return (ValidatedSignedDoc) oin.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
示例8: setGalleryAppImage
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
/**
* when an app is published/updated, we need to move the image
* that was temporarily uploaded into projects/projectid/image
* into the gallery image
* @param app gallery app
*/
private void setGalleryAppImage(GalleryApp app) {
// best thing would be if GCS has a mv op, we can just do that.
// don't think that is there, though, so for now read one and write to other
// First, read the file from projects name
boolean lockForRead = false;
//String projectImageKey = app.getProjectImageKey();
GallerySettings settings = loadGallerySettings();
String projectImageKey = settings.getProjectImageKey(app.getProjectId());
try {
GcsService gcsService = GcsServiceFactory.createGcsService();
//GcsFilename filename = new GcsFilename(GalleryApp.GALLERYBUCKET, projectImageKey);
GcsFilename filename = new GcsFilename(settings.getBucket(), projectImageKey);
GcsInputChannel readChannel = gcsService.openReadChannel(filename, 0);
InputStream gcsis = Channels.newInputStream(readChannel);
byte[] buffer = new byte[8000];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = gcsis.read(buffer)) != -1) {
bao.write(buffer, 0, bytesRead);
}
// close the project image file
readChannel.close();
// if image is greater than 200 X 200, it will be scaled (200 X 200).
// otherwise, it will be stored as origin.
byte[] oldImageData = bao.toByteArray();
byte[] newImageData;
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
//if image size is too big, scale it to a smaller size.
if(oldImage.getWidth() > 200 && oldImage.getHeight() > 200){
Transform resize = ImagesServiceFactory.makeResize(200, 200);
Image newImage = imagesService.applyTransform(resize, oldImage);
newImageData = newImage.getImageData();
}else{
newImageData = oldImageData;
}
// set up the cloud file (options)
// After publish, copy the /projects/projectId image into /apps/appId
//String galleryKey = app.getImageKey();
String galleryKey = settings.getImageKey(app.getGalleryAppId());
//GcsFilename outfilename = new GcsFilename(GalleryApp.GALLERYBUCKET, galleryKey);
GcsFilename outfilename = new GcsFilename(settings.getBucket(), galleryKey);
GcsFileOptions options = new GcsFileOptions.Builder().mimeType("image/jpeg")
.acl("public-read").cacheControl("no-cache").build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(outfilename, options);
writeChannel.write(ByteBuffer.wrap(newImageData));
// Now finalize
writeChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
LOG.log(Level.INFO, "FAILED WRITING IMAGE TO GCS");
e.printStackTrace();
}
}
示例9: zipFiles
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
/**
* Method used to ZIP a Google Cloud Services file array to a predefined
* Google Cloud Services file Zip
*
* @author Rodrigo Cabrera ([email protected])
* @param filesToZip
* the array of Google Cloud Services files that points to the
* zip
* @since 22 / feb / 2016
*/
private void zipFiles(final GcsFilename... filesToZip) throws IOException {
logger.debug("zipping files " + filesToZip.length);
GcsFilename targetZipFile = new GcsFilename(BUCKET_NAME, GTFS_FILE);
final int fetchSize = 4 * 1024 * 1024;
final int readSize = 2 * 1024 * 1024;
GcsOutputChannel outputChannel = null;
ZipOutputStream zip = null;
try {
GcsFileOptions options = new GcsFileOptions.Builder().mimeType(MediaType.ZIP.toString()).build();
outputChannel = gcsService.createOrReplace(targetZipFile, options);
zip = new ZipOutputStream(Channels.newOutputStream(outputChannel));
GcsInputChannel readChannel = null;
for (GcsFilename file : filesToZip) {
logger.debug("zipping file: " + file);
try {
final GcsFileMetadata meta = gcsService.getMetadata(file);
if (meta == null) {
continue;
}
ZipEntry entry = new ZipEntry(file.getObjectName());
zip.putNextEntry(entry);
readChannel = gcsService.openPrefetchingReadChannel(file, 0, fetchSize);
final ByteBuffer buffer = ByteBuffer.allocate(readSize);
int bytesRead = 0;
while (bytesRead >= 0) {
bytesRead = readChannel.read(buffer);
buffer.flip();
zip.write(buffer.array(), buffer.position(), buffer.limit());
buffer.rewind();
buffer.limit(buffer.capacity());
}
} finally {
logger.debug("closing file");
zip.closeEntry();
readChannel.close();
}
}
} finally {
zip.flush();
zip.close();
outputChannel.close();
}
}
示例10: read
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
public static InputStream read(final String stringFileName) throws IOException {
final GcsInputChannel readChannel = gcsService.openPrefetchingReadChannel( open(stringFileName) , 0, 1024 * 1024);
return Channels.newInputStream(readChannel);
}
示例11: readFromFile
import com.google.appengine.tools.cloudstorage.GcsInputChannel; //导入依赖的package包/类
/**
* Method used to read the contents of a Google Cloud Services file to a
* byte array
*
* @author Rodrigo Cabrera ([email protected])
* @param fileName
* the Google Cloud Services file that points to the file to
* write
* @return the byte array that contains the contents
* @since 22 / feb / 2016
*/
private byte[] readFromFile(GcsFilename fileName) throws IOException {
int fileSize = (int) gcsService.getMetadata(fileName).getLength();
ByteBuffer result = ByteBuffer.allocate(fileSize);
try (GcsInputChannel readChannel = gcsService.openReadChannel(fileName, 0)) {
readChannel.read(result);
}
return result.array();
}