我使用watchbucket命令观看gcs存储桶。之后,我正在上传。 watch命令会在应用程序引擎上向我的servlet发送通知。为了确保这一点,我看看日志。但似乎是这样,一些servlet如何在请求后继续获取请求。成功四次,之后它得到一个空指针异常。为什么有这么多的要求?将图片上传到Google云端存储后请求过多
*编辑* 在客户端我使用meteorJS Javascript框架。我添加了扩展弹弓来处理上传。 首先我必须提供类似的ACL,水桶等这样的必要信息:
Slingshot.createDirective('uploadSpotCover', Slingshot.GoogleCloud, {
bucket: 'upload_spot_cover',
GoogleAccessId: 'accessId',
acl: 'project-private',
maxSize: 0,
cacheControl: 'no-cache',
...
}
正如你可以看到线153弹弓使用一个XMLHttpRequest上传到云端储存 https://github.com/CulturalMe/meteor-slingshot/blob/master/lib/upload.js#L144
在服务器端我的Servlet和它的逻辑是这样的。
公共类上传延伸的HttpServlet {
private BucketNotification notification;
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
this.notification = getBucketNotification(req);
UploadObject object = new UploadObject(notification);
CloudStorageHandler gcs = new CloudStorageHandler();
BlobstoreHandler bs = new BlobstoreHandler();
ImageTransformHandler is = new ImageTransformHandler();
/** GET DATA FROM GCS **/
byte[] data = gcs.getFileFromGoogleCloudStorage(object.getGcsBucket(), object.getGcsPath());
//BlobKey bk = bs.createBlobKey(object.getGcsBucket(), object.getGcsPath());
/******************/
/** TRANSFORMATION **/
byte[] newImageData = is.resizePicture(data, 1200, 1200, "JPEG", 65, 0.5, 0.5);
/******************/
/** STORE NEW RESIZED FILE INTO GCS BUCKET **/
UploadObject tmp = new UploadObject(object.getGcsPath(), "JPEG", "beispiel", 1200, 1200);
tmp.setData(newImageData);
gcs.saveFileToGoogleCloudStorage(newImageData, "beispiel", object.getGcsPath(), "JPEG", "public-read");
/******************/
/** CREATE SERVING URL via BLOBKEY **/
BlobKey bk_neu = bs.createBlobKey("beispiel", object.getGcsPath());
String servingUrl = is.createServingUrlWithBlobkey(bk_neu);
/******************/
log("Blobkey: "+ bk_neu.getKeyString());
log("url: "+ servingUrl);
res.setStatus(200);
}
private BucketNotification getBucketNotification(HttpServletRequest req) {
BucketNotification notification = null;
String jsonString ="";
try {
jsonString ="";
BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));
for (String buffer;(buffer = in.readLine()) != null;jsonString+=buffer + "\n");
in.close();
notification = new Gson().fromJson(jsonString, BucketNotification.class);
} catch (IOException e) {
log("Failed to decode the notification: " + e.getMessage());
return null;
}
return notification;
}
}
我包的特定服务的方法,如保存的文件在其自己的处理程序,云端储存。
我发现这对[链接](https://cloud.google.com/storage/docs/gsutil/addlhelp/RetryHandlingStrategy): [...] 因此,在默认情况下,gsutil可在重试23次1 + 2 + 4 + 8 + 16 + 32 + 60 ...秒约10分钟。您可以通过编辑.boto配置文件的“[Boto]”部分中的num_retries和max_retry_delay配置变量来调整任何单个重试的重试次数和最大延迟时间。大多数用户不需要更改这些值。 [...] 所以我去了我的.boto文件,并将num_entries更改为1,因为不允许为0。不幸的是没有改变 – Laslo89