Skip to content

Instantly share code, notes, and snippets.

@bjorn
Last active December 17, 2015 00:19
Show Gist options
  • Save bjorn/5520273 to your computer and use it in GitHub Desktop.
Save bjorn/5520273 to your computer and use it in GitHub Desktop.
libcloud based scripts
#!/usr/bin/env python2
#
# Warning: this script will delete the given container, including its contents
#
from argparse import ArgumentParser
from os import environ
from sys import exit
from libcloud.storage.types import Provider, ContainerDoesNotExistError
from libcloud.storage.providers import get_driver
parser = ArgumentParser()
parser.add_argument("container")
args = parser.parse_args()
try:
RACKSPACE_USER = environ['RACKSPACE_USER']
RACKSPACE_KEY = environ['RACKSPACE_KEY']
except KeyError:
print("Please set the environment variables RACKSPACE_USER and "
"RACKSPACE_KEY")
exit(1)
driver = get_driver(Provider.CLOUDFILES_UK)(RACKSPACE_USER, RACKSPACE_KEY)
try:
container = driver.get_container(args.container)
except ContainerDoesNotExistError:
print('container ' + args.container + ' not found')
exit(1)
# TODO: Check whether container is empty, and only delete the objects when
# command-line argument -f / --force is given.
for obj in container.list_objects():
print("deleting object " + obj.name)
obj.delete()
print("deleting container " + container.name)
container.delete()
#!/usr/bin/env python2
#
# Warning: this script will delete all objects from the given container
#
from argparse import ArgumentParser
from os import environ
from sys import exit
from libcloud.storage.types import Provider, ContainerDoesNotExistError
from libcloud.storage.providers import get_driver
parser = ArgumentParser()
parser.add_argument("container")
args = parser.parse_args()
try:
RACKSPACE_USER = environ['RACKSPACE_USER']
RACKSPACE_KEY = environ['RACKSPACE_KEY']
except KeyError:
print("Please set the environment variables RACKSPACE_USER and "
"RACKSPACE_KEY")
exit(1)
driver = get_driver(Provider.CLOUDFILES_UK)(RACKSPACE_USER, RACKSPACE_KEY)
try:
container = driver.get_container(args.container)
except ContainerDoesNotExistError:
print('container ' + args.container + ' not found')
exit(1)
# TODO: Check whether container is empty, and only delete the objects when
# command-line argument -f / --force is given.
for obj in container.list_objects():
print("deleting object " + obj.name)
obj.delete()
#!/usr/bin/env python2
#
# This script lists the objects in a container along with their content type.
#
from argparse import ArgumentParser
from os import environ
from sys import exit
from libcloud.storage.types import Provider, ContainerDoesNotExistError
from libcloud.storage.providers import get_driver
parser = ArgumentParser()
parser.add_argument("container")
args = parser.parse_args()
try:
RACKSPACE_USER = environ['RACKSPACE_USER']
RACKSPACE_KEY = environ['RACKSPACE_KEY']
except KeyError:
print("Please set the environment variables RACKSPACE_USER and "
"RACKSPACE_KEY")
exit(1)
driver = get_driver(Provider.CLOUDFILES_UK)(RACKSPACE_USER, RACKSPACE_KEY)
try:
container = driver.get_container(args.container)
except ContainerDoesNotExistError:
print('container ' + args.container + ' not found')
exit(1)
for obj in container.list_objects():
print(obj.name + ' (' + str(obj.extra['content_type'] + ')'))
#!/usr/bin/env python2
#
# Uploads the given files to the given container.
#
import os
from argparse import ArgumentParser
from sys import exit
from libcloud.storage.types import Provider, ContainerDoesNotExistError
from libcloud.storage.providers import get_driver
parser = ArgumentParser()
parser.add_argument("container", metavar='CONTAINER', help='container to upload to')
parser.add_argument("files", metavar='FILE', nargs='+', help='file to upload')
args = parser.parse_args()
try:
RACKSPACE_USER = os.environ['RACKSPACE_USER']
RACKSPACE_KEY = os.environ['RACKSPACE_KEY']
except KeyError:
print("Please set the environment variables RACKSPACE_USER and "
"RACKSPACE_KEY")
exit(1)
driver = get_driver(Provider.CLOUDFILES_UK)(RACKSPACE_USER, RACKSPACE_KEY)
try:
container = driver.get_container(args.container)
except ContainerDoesNotExistError:
# TODO: May be nice to create the container with --create parameter
print('container ' + args.container + ' not found')
exit(1)
# Explicitly specify some content types that libcloud is not detecting
def content_type(file):
if file.endswith('.tmx') or file.endswith('.tsx'):
return 'application/xml'
if file.endswith('.png'):
return 'image/png'
if file.endswith('.ogg'):
return 'audio/ogg'
return None
for file in args.files:
print("uploading " + file)
extra = { 'content_type': content_type(file) }
container.upload_object(file_path=file, object_name=file, extra=extra)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment