GithubHelp home page GithubHelp logo

deems / selectel-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from go1dshtein/selectel-api

0.0 1.0 0.0 106 KB

python client library for selectel api

License: GNU General Public License v2.0

Python 100.00%

selectel-api's Introduction

pypi-version

selectel-api

Simple python module which provide access to selectel cloud services.

Selectel Cloud Storage

Creating and dropping containers

For handle containers you should use global selectel credentials:

>>> from selectel.storage import Storage
>>> storage = Storage(auth, key)

You can create public and private containers with specific headers:

>>> storage.create("mycontainer")
>>> headers = {"X-Container-Meta-Access-Control-Request-Method": "HEAD, GET"}
>>> storage.create("mypubliccontainer", public=True, headers=headers)

and drop it:

>>> storage.drop("mypubliccontainer")

Use storage.drop("mypubliccontainer", recursive=True) for dropping nonempty container.

Working with objects inside container

For working with objects you can use container specific credentials:

>>> from selectel.storage import Container
>>> container = Container(auth_container, key_container, "mycontainer")

You can add objects from content:

>>> container.put("/test1.file", b"something")

from file:

>>> container.put_file("/test2.file", "./test.zip")

or from readable stream:

>>> container.put_stream("/dir/test3.file", open("./test.zip", "r+b"))

Optionally you can add specific headers:

>>> container.put_stream("/dir/test3.file", open("./test.zip", "r+b"), headers={"Content-Type": "application/zip"})

Also you can use put* commands to extract archives(tar, tar.gz, tar.bz2) to container, e.g.:

>>> container.put_file("/dir", "archive.tar.gz", extract="tar.gz")
(18, [])

In this case put command returns count of created(overwritten) files and list of errors.

API allows to get some info about container and objects inside it:

>>> container.list().keys()
[u'/test1.file', u'/dir/test3.file', u'/test2.file']
>>> container.list("/dir").keys()
[u'/dir/test3.file']
>>> container.list("/").keys()
[u'/test1.file', u'/test2.file']
>>> container.list("/dir")["/dir/test3.file"]
{'hash': u'29dc385f67f38be9bddac38785aea25d', 'content-length': 101974, 'content-type': u'application/zip', 'last-modified': datetime.datetime(2013, 11, 12, 13, 31, 56, 527830)}
>>> container.info()
{'count': 3, 'usage': 203957, 'rx': 512219, 'public': False, 'tx': 126390}
>>> container.info("/dir/test3.file")
{'downloads': 0, 'last-modified': datetime.datetime(2013, 11, 12, 13, 31, 56), 'content-length': 101974, 'hash': '29dc385f67f38be9bddac38785aea25d', 'content-type': 'application/zip'}

You can get content of object as string:

>>> container.get("/test1.file")
'something'

or as stream:

>>> for chunk in container.get_stream("/test2.file", chunk=2**15):
...     print len(chunk)
...
32768
32768
32768
3670

And last that you can do:

>>> container.copy("/dir/test3.file", "/test4.file")
>>> container.list("/").keys()
[u'/test4.file', u'/test1.file', u'/test2.file']
>>> container.remove("/test1.file")
>>> container.list("/").keys()
[u'/test4.file', u'/test2.file']

Running tests

Add file tests/credentials.json with your credentials:

{
	"auth": "my-selectel-login",
	"key": "my-selectel-password"
}

Then, run

tox

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.