Ruby google存储API改变共享PUBLICLY

时间:2014-09-18 22:05:51

标签: ruby google-cloud-storage google-api-ruby-client

我对ruby google API有疑问。

我试图使用Google存储来托管图片。我想让每个用户都能够阅读图像。到目前为止,我可以连接到谷歌云存储并将图像上传到存储。但是,我无法通过API更改权限,我使用的代码如下:

 def build_client
  client = Google::APIClient.new()

  key = Google::APIClient::PKCS12.load_key('client.p12', 'notasecret')

  service_account = Google::APIClient::JWTAsserter.new(
      '553281084416-og1purkefp0md0g9n5kvlko8e74qm42v@developer.gserviceaccount.com',
      'https://www.googleapis.com/auth/devstorage.full_control',
  key)

  client.authorization = service_account.authorize
  client
end

def send_image_file(file_path, upload_file_name, client)
    storage = client.discovered_api('storage', 'v1beta2')
    media = Google::APIClient::UploadIO.new(file_path, 'image/*')
    result = client.execute(
      api_method: storage.objects.insert,
      media: media,
      parameters: {
        uploadType: 'media',
        predefinedAcl: 'publicRead',
        bucket: 'portlight_images',
        name: upload_file_name,
        projection: 'full'
      }
    )
    result
end

我使用(predefinedAcl:' publicRead')来更改ACL,但它不会做任何事情。当我想使用以下网址时:" https://console.developers.google.com/m/cloudstorage/b/# {bucket} / o /#{object}"为了访问图像,我必须手动点击存储控制台上的按钮,使其“共享”#34;共享"共享。

我想知道有什么方法可以通过API将链接公开给每个人。 感谢。

1 个答案:

答案 0 :(得分:0)

你可能已经解决了这个问题,但只针对遇到同样问题的其他人: 您必须在client.execute中设置body对象中的acl键和值。

result = client.execute(
  api_method: storage.objects.insert,
  media: media,
  parameters: {
    uploadType: 'multipart',
    bucket: 'portlight_images',
    name: upload_file_name,
    projection: 'full',
    acl: 'public-read'
  },
 :body_object => {
    'acl' => [{
        'entity' => 'allUsers',
        'role' => 'READER'  
    }],
    'bucket' => portlight_images,
    'object' => upload_file_name
  }
)