2014-09-18 42 views
0

我有一個關於紅寶石谷歌API的問題。紅寶石谷歌存儲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(
     '[email protected]account.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}」這樣的URL訪問映像時,我必須手動點擊存儲控制檯上的按鈕才能使其「共享共享」。

我想知道是否有任何方法可以通過API將鏈接公開給大家。 謝謝。

回答

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 
    } 
) 
+0

是的,這一個工作,我發現我的工作也在幾天後,但你的似乎更好,謝謝! – 2015-03-25 19:21:12