Created
January 30, 2016 01:52
-
-
Save deanputney/5c2df8b85c299ded3a3d to your computer and use it in GitHub Desktop.
Example for apache-libcloud ACL setting failure with Google Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Insert your service account email and key | |
# Provide a project ID and destination bucket | |
# ACL for inserted object should be "public-read" | |
# Content-Type for inserted object should be "text/plain" | |
import sys | |
import cStringIO | |
from libcloud.storage.types import Provider | |
from libcloud.common.types import LibcloudError | |
from libcloud.storage.providers import get_driver | |
GOOGLE_CLIENT_EMAIL = "" # client email | |
GOOGLE_PRIVATE_KEY = "" # private key | |
project = "" # google project ID | |
bucket = "" # bucket name | |
domain_path = 'https://'+bucket+'.storage.googleapis.com/' | |
cls = get_driver(Provider.GOOGLE_STORAGE) | |
driver = cls(GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY) | |
container = driver.get_container(container_name=bucket) | |
def store_stream(stream, file_name, metadata=None): | |
extra = { | |
'acl': 'public-read', # provide a predefinedAcl (not working) | |
'content_type': 'text/plain' # provide a content_type (since it doesn't work) | |
} | |
obj = driver.upload_object_via_stream(iterator=stream, | |
container=container, | |
object_name=file_name, | |
extra=extra) | |
if obj.name is not None and obj.name != "": | |
resp = domain_path+obj.name | |
return resp | |
return None | |
# metadata can be a dict with string keys and values | |
def store_string(string, file_name, metadata=None): | |
return store_stream(cStringIO.StringIO(string), file_name, metadata) | |
# Store a simple string-based file | |
print store_string("hey", "hey.txt") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment