【发布时间】:2016-06-09 15:40:01
【问题描述】:
我正在应用引擎端点中创建signedURL,然后将其提供给客户端。但是当客户端尝试使用签名 URL 上传时,云存储会抛出以下错误
Access denied. Anonymous users does not have storage.objects.create access to bucket
生成签名URL的应用引擎代码如下:
private String getSignedUrl() {
String encodedUrl = null;
String httpVerb = "PUT";
String contentMD5 = "";
String contentType = "image/rgb";
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 10);
long expiration = calendar.getTimeInMillis() / 1000L;
String canonicalizedExtensionHeaders = "";
String canonicalizedResource =
"/<bucket_name>/<folder_name>/";
String stringToSign =
httpVerb + "\n" + contentMD5 + "\n" + contentType + "\n"
+ expiration + "\n" + canonicalizedExtensionHeaders
+ canonicalizedResource;
AppIdentityService service =
AppIdentityServiceFactory.getAppIdentityService();
String googleAccessId = service.getServiceAccountName();
String baseURL =
"http://storage.googleapis.com/<bucket_name>/<folder-name>/";
SigningResult signingResult =
service.signForApp(stringToSign.getBytes());
String encodedSignature = null;
try {
encodedSignature =
URLEncoder.encode(
new String(Base64.encodeBase64(
signingResult.getSignature(), false),
"UTF-8"), "UTF-8").toString();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
encodedUrl =
baseURL + "?GoogleAccessId=" + googleAccessId + "&Expires="
+ expiration + "&Signature=" + encodedSignature;
return encodedUrl;
}
在获得签名的 URL 后,我正在使用 cURL 来测试上传。我使用以下命令上传文件
curl -X PUT -H "Content-Type: multipart/form" -F file=@"<file_path>";type=image/rgb <signed_url>
我在 cURL 中尝试了 POST 和 PUT,结果相同。 我在这里遗漏了什么吗?
【问题讨论】:
-
我不知道这是否是问题所在,但通常您可能希望为 HTTPS 而不是 HTTP 签署 URL。
-
即使使用 https 并根据@Andrei 的建议删除文件夹名称后,我也无法从客户端上传。
标签: google-app-engine google-cloud-storage