是否可以使用 appengine Api 更改 Google Cloud Storage 对象(或存储桶)的 ACL?我知道这可以使用 REST API 来完成,但是在 appengine 的 Files Api 中是否支持这一点?可以在使用 GSFileObject 创建新对象时设置它们,但是您可以更改现有对象吗?
问问题
1339 次
2 回答
9
您可以使用urlfetch.fetch和app_identity.get_access_token轻松地将经过身份验证的请求发送到 REST api。
Python:
from google.appengine.api import app_identity
from google.appengine.api import urlfetch
acl_xml = """
<AccessControlList><Entries>
<Entry>
<Scope type="UserByEmail">foo@example.com</Scope>
<Permission>READ</Permission>
</Entry>
</Entries></AccessControlList>
"""
scope = 'https://www.googleapis.com/auth/devstorage.full_control'
token = app_identity.get_access_token(scope)
response = urlfetch.fetch(
'http://storage.googleapis.com/bucket/obj?acl',
method=urlfetch.PUT,
payload=acl_xml,
headers={'Authorization': 'OAuth %s' % token})
爪哇:
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public String setAcl() throws Exception {
// Change foo@example.com to a valid email.
// Repeat <Entry/> as many times as necessary.
String xmlString = "";
xmlString += "<AccessControlList><Entries>";
xmlString += " <Entry>";
xmlString += " <Scope type=\"UserByEmail\">foo@example.com</Scope>";
xmlString += " <Permission>READ</Permission>";
xmlString += " </Entry>";
xmlString += "</Entries></AccessControlList>";
ArrayList scopes = new ArrayList();
scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
AppIdentityService.GetAccessTokenResult accessToken =
AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes);
// Change bucket and obj to the bucket and object of interest.
URL url = new URL("https://storage.googleapis.com/bucket/obj?acl");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.addRequestProperty(
"Authorization", "OAuth " + accessToken.getAccessToken());
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(xmlString);
writer.close();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new Exception();
}
}
更多信息:
于 2012-11-29T00:00:16.247 回答
1
App Engine Google Cloud Storage API 不支持修改现有对象的 ACL,但是,我刚刚编写了一个功能请求,要求添加该功能。
于 2012-11-28T23:58:45.627 回答