本文整理汇总了Java中com.google.appengine.api.blobstore.BlobKey类的典型用法代码示例。如果您正苦于以下问题:Java BlobKey类的具体用法?Java BlobKey怎么用?Java BlobKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BlobKey类属于com.google.appengine.api.blobstore包,在下文中一共展示了BlobKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: slurp
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
private static byte[] slurp(BlobKey blobKey) throws IOException {
FileReadChannel in = getFileService().openReadChannel(
new AppEngineFile(AppEngineFile.FileSystem.BLOBSTORE, blobKey.getKeyString()),
false);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteBuffer buf = ByteBuffer.allocate(BUFFER_BYTES);
while (true) {
int bytesRead = in.read(buf);
if (bytesRead < 0) {
break;
}
Preconditions.checkState(bytesRead != 0, "0 bytes read: %s", buf);
out.write(buf.array(), 0, bytesRead);
buf.clear();
}
return out.toByteArray();
}
示例2: parse
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Override
protected AttachmentMetadata parse(Entity e) {
AttachmentId id = new AttachmentId(e.getKey().getName());
String blobKey = DatastoreUtil.getOptionalProperty(e, BLOB_KEY_PROPERTY, String.class);
if (blobKey == null) {
// For some legacy data, the attachment id is the blob key.
log.info("Legacy attachment without blob key: " + id);
blobKey = id.getId();
}
String metadata = DatastoreUtil.getExistingProperty(e, JSON_METADATA_PROPERTY, String.class);
try {
return new AttachmentMetadata(id, new BlobKey(blobKey), new JSONObject(metadata));
} catch (JSONException je) {
throw new DatastoreUtil.InvalidPropertyException(e, JSON_METADATA_PROPERTY,
"Invalid json metadata: " + metadata, je);
}
}
示例3: serveDownload
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
/**
* Serves the attachment with cache control.
*
* @param req Only used to check the If-Modified-Since header.
*/
public void serveDownload(AttachmentId id,
HttpServletRequest req, HttpServletResponse resp) throws IOException {
if (maybeCached(req, resp, "download, id=" + id)) {
return;
}
AttachmentMetadata metadata = getMetadata(id);
if (metadata == null) {
throw NotFoundException.withInternalMessage("Attachment id unknown: " + id);
}
BlobKey key = metadata.getBlobKey();
BlobInfo info = new BlobInfoFactory().loadBlobInfo(key);
String disposition = "attachment; filename=\""
// TODO(ohler): Investigate what escaping we need here, and whether the
// blobstore service has already done some escaping that we need to undo
// (it seems to do percent-encoding on " characters).
+ info.getFilename().replace("\"", "\\\"").replace("\\", "\\\\")
+ "\"";
log.info("Serving " + info + " with Content-Disposition: " + disposition);
resp.setHeader("Content-Disposition", disposition);
blobstore.serve(key, resp);
}
示例4: turnBlobIntoAttachment
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
public AttachmentId turnBlobIntoAttachment(BlobKey blobKey) throws IOException {
Preconditions.checkNotNull(blobKey, "Null blobKey");
AttachmentId newId = new AttachmentId(random64.next(
// 115 * 6 random bits; should be unguessable. (6 bits per random64 char.)
115));
Assert.check(metadataDirectory.getWithoutTx(newId) == null,
"Random attachment id already taken: %s", newId);
log.info("Computing metadata for " + newId + " (" + blobKey + ")");
AttachmentMetadata metadata = computeMetadata(newId, blobKey);
AttachmentMetadata existingMetadata = metadataDirectory.getOrAdd(metadata);
if (existingMetadata != null) {
// This is expected if, during getOrAdd, a commit times out from our
// perspective but succeeded in the datatstore, and we notice the existing
// data during a retry. Still, we log severe until we confirm that this
// is indeed harmless.
log.severe("Metadata for new attachment " + metadata
+ " already exists: " + existingMetadata);
}
log.info("Wrote metadata " + metadata);
return newId;
}
示例5: attemptGetImageMetadata
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Nullable private Image attemptGetImageMetadata(BlobstoreService blobstore, BlobInfo info) {
if (info.getSize() == 0) {
// Special case since it would lead to an IllegalArgumentException below.
log.info("Empty attachment, can't get image metadata: " + info);
return null;
}
final int readPortion = headerBytesUpperBound;
BlobKey key = info.getBlobKey();
byte[] data = blobstore.fetchData(key, 0, readPortion);
try {
Image img = ImagesServiceFactory.makeImage(data);
// Force the image to be processed
img.getWidth();
img.getHeight();
return img;
} catch (RuntimeException e) {
log.log(Level.SEVERE, "Problem getting image metadata; ignoring", e);
return null;
}
}
示例6: serveFileFromCloudStorage
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
public static void serveFileFromCloudStorage(String bucketName,
String objectName,
String contentType,
HttpServletResponse resp) throws IOException {
// GcsFilename fileName = getFileName(req);
GcsFilename fileName = new GcsFilename(bucketName, objectName);
// resp.setContentType("video/webm");
resp.setContentType(contentType);
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: doPost
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get("moment");
if (blobKeys != null && !blobKeys.isEmpty()) {
res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("application/json");
JSONObject json = new JSONObject();
json.put("blob-key", blobKeys.get(0).getKeyString());
PrintWriter out = res.getWriter();
out.print(json.toString());
out.flush();
out.close();
}
}
示例8: getMoment
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Test
public void getMoment() throws Exception {
MomentRecord moment = new MomentRecord()
.setBlobKey(new BlobKey(BLOB_KEY))
.setSenderId(otherUserRecord.getId())
.setSenderName(otherUserRecord.getDisplayName())
.setRecipients(new ArrayList<Long>() {{
add(currentUserRecord.getId());
}})
.setType(MomentType.DRAWING);
ofy().save().entity(moment).now();
MomentResponse response = sut.get(moment.getId(), CURRENT_USER);
assertNotNull(response);
assertEquals(moment.getSenderId(), response.getSenderId());
assertEquals(moment.getSenderName(), response.getSenderName());
assertEquals(moment.getType(), response.getMomentType());
assertEquals(FakeImagesService.SERVING_URL, response.getServingUrl());
}
示例9: getMomentAsTheSender
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Test
public void getMomentAsTheSender() throws Exception {
MomentRecord moment = new MomentRecord()
.setBlobKey(new BlobKey(BLOB_KEY))
.setSenderId(currentUserRecord.getId())
.setSenderName(currentUserRecord.getDisplayName())
.setRecipients(new ArrayList<Long>() {{
add(otherUserRecord.getId());
}})
.setType(MomentType.DRAWING);
ofy().save().entity(moment).now();
MomentResponse response = sut.get(moment.getId(), CURRENT_USER);
assertNotNull(response);
assertEquals(moment.getSenderId(), response.getSenderId());
assertEquals(moment.getSenderName(), response.getSenderName());
assertEquals(moment.getType(), response.getMomentType());
assertEquals(FakeImagesService.SERVING_URL, response.getServingUrl());
}
示例10: getAllSent
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Test
public void getAllSent() throws Exception {
MomentRecord moment = new MomentRecord()
.setBlobKey(new BlobKey(BLOB_KEY))
.setSenderId(currentUserRecord.getId())
.setRecipients(new ArrayList<Long>() {{
add(otherUserRecord.getId());
}})
.setType(MomentType.DRAWING)
.setCreated(new Date());
ofy().save().entity(moment).now();
List<MomentResponse> items = (List<MomentResponse>) sut.allSent(CURRENT_USER).getItems();
assertEquals(1, items.size());
assertNotNull(items.get(0));
assertEquals(moment.getType(), items.get(0).getMomentType());
assertEquals(FakeImagesService.SERVING_URL, items.get(0).getServingUrl());
}
示例11: getAllReceived
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Test
public void getAllReceived() throws Exception {
MomentRecord moment = new MomentRecord()
.setBlobKey(new BlobKey(BLOB_KEY))
.setSenderId(otherUserRecord.getId())
.setSenderName(otherUserRecord.getDisplayName())
.setRecipients(new ArrayList<Long>() {{
add(currentUserRecord.getId());
}})
.setType(MomentType.DRAWING)
.setCreated(new Date());
ofy().save().entity(moment).now();
List<MomentResponse> items = (List<MomentResponse>) sut.allReceived(CURRENT_USER).getItems();
assertEquals(1, items.size());
assertNotNull(items.get(0));
assertEquals(moment.getSenderId(), items.get(0).getSenderId());
assertEquals(moment.getSenderName(), items.get(0).getSenderName());
assertEquals(moment.getType(), items.get(0).getMomentType());
assertEquals(FakeImagesService.SERVING_URL, items.get(0).getServingUrl());
}
示例12: doGet
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
BlobKey blobKey = new BlobKey(req.getParameter("blobkey"));
String maxLengthString = req.getParameter("s");
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions servingUrlOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
if (maxLengthString == null || maxLengthString.isEmpty()) {
//res.sendRedirect(imagesService.getServingUrl(blobKey));
res.sendRedirect(imagesService.getServingUrl(servingUrlOptions));
}
else {
int maxLength = Integer.parseInt(maxLengthString);
//res.sendRedirect(imagesService.getServingUrl(blobKey, maxLength, false));
res.sendRedirect(imagesService.getServingUrl(servingUrlOptions.imageSize(maxLength)));
}
}
示例13: StationAudioSimple
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
/**
* StationAudioSimple constructor.
* @param stationAudioKey
* : stationAudio key
* @param stationAudioType
* : stationAudio type
* @param stationAudioName
* : stationAudio name
* @param stationAudioMultimediaContent
* : stationAudio Multimedia Content BlobKey
* @param stationAudioDuration
* : stationAudio duration
* @param stationAudioFormat
* : stationAudio format
*/
public StationAudioSimple(String stationAudioKey,
String stationAudioType,
String stationAudioName,
BlobKey stationAudioMultimediaContent,
Double stationAudioDuration,
String stationAudioFormat
) {
this.stationAudioKey = stationAudioKey;
this.stationAudioType = stationAudioType;
this.stationAudioName = stationAudioName;
this.stationAudioMultimediaContent = stationAudioMultimediaContent;
this.stationAudioDuration = stationAudioDuration;
this.stationAudioFormat = stationAudioFormat;
}
示例14: assignBlobKey
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
/**
* Returns a new blobkey for an uploaded blob.
* If no blob was uploaded, then null is returned.
* @param req
* : the HTTP Servlet Request from the Servlet
* @param paramName
* : the name of the HTTP blob parameter
* @param blobStoreService
* : the blobstore service initialized in the calling servlet
* @return a new BlobKey for this blob
*/
public static BlobKey assignBlobKey(HttpServletRequest req, String paramName,
BlobstoreService blobstoreService) {
java.util.Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get(paramName);
if (blobKeys != null) {
BlobKey blobKey = blobKeys.get(0);
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobKey != null) {
if (blobInfo.getSize() > 0) {
return blobKey;
}
else {
blobstoreService.delete(blobKey);
return null;
}
}
else {
return null;
}
}
else {
return null;
}
}
示例15: doPost
import com.google.appengine.api.blobstore.BlobKey; //导入依赖的package包/类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String title = req.getParameter("title");
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
BlobKey blobKey = blobs.get("myFile").get(0);
BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobKey == null) {
resp.sendRedirect("/admin/resource/add.jsp?error");
}
Resource resource = new Resource();
resource.setTitle(title);
resource.setBlobKey(blobKey);
resource.setContentType(info.getContentType());
resource.setFileName(info.getFilename());
resource.setSize(info.getSize());
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(resource);
pm.close();
resp.sendRedirect("/admin/resource/");
}