Skip to content

Instantly share code, notes, and snippets.

@deanputney
Created January 30, 2016 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deanputney/5c2df8b85c299ded3a3d to your computer and use it in GitHub Desktop.
Save deanputney/5c2df8b85c299ded3a3d to your computer and use it in GitHub Desktop.
Example for apache-libcloud ACL setting failure with Google Storage
# 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