从Appengine(JAVA)更改Google云存储的ACL

时间:2012-11-28 23:04:22

标签: google-app-engine acl google-cloud-storage

是否可以使用appengine Api更改Google Cloud Storage对象(或存储桶)的ACL?我知道这可以使用REST API完成,但是在appengine的Files Api中是否支持这一点?可以在使用GSFileObject创建新对象时设置它们,但是可以更改现有对象吗?

2 个答案:

答案 0 :(得分:9)

您可以使用urlfetch.fetchapp_identity.get_access_token轻松地将authenticated request发送到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();
  }
}

更多信息:

答案 1 :(得分:1)

App Engine Google Cloud Storage API不支持修改现有对象的ACL,但是,我刚刚编写了一个要求添加该功能的功能请求。