File: getting_started.rst

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (187 lines) | stat: -rw-r--r-- 6,310 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
.. _getting-started:

=========================
Getting Started with Boto
=========================

This tutorial will walk you through installing and configuring ``boto``, as
well how to use it to make API calls.

This tutorial assumes you are familiar with Python & that you have registered
for an `Amazon Web Services`_ account. You'll need retrieve your
``Access Key ID`` and ``Secret Access Key`` from the web-based console.

.. _`Amazon Web Services`: https://aws.amazon.com/


Installing Boto
---------------

You can use ``pip`` to install the latest released version of ``boto``::

    pip install boto

If you want to install ``boto`` from source::

    git clone git://github.com/boto/boto.git
    cd boto
    python setup.py install

.. note::

    For most services, this is enough to get going. However, to support
    everything Boto ships with, you should additionally run
    ``pip install -r requirements.txt``.

    This installs all additional, non-stdlib modules, enabling use of things
    like ``boto.cloudsearch``, ``boto.manage`` & ``boto.mashups``, as well as
    covering everything needed for the test suite.


Using Virtual Environments
--------------------------

Another common way to install ``boto`` is to use a ``virtualenv``, which
provides isolated environments. First, install the ``virtualenv`` Python
package::

    pip install virtualenv

Next, create a virtual environment by using the ``virtualenv`` command and
specifying where you want the virtualenv to be created (you can specify
any directory you like, though this example allows for compatibility with
``virtualenvwrapper``)::

    mkdir ~/.virtualenvs
    virtualenv ~/.virtualenvs/boto

You can now activate the virtual environment::

    source ~/.virtualenvs/boto/bin/activate

Now, any usage of ``python`` or ``pip`` (within the current shell) will default
to the new, isolated version within your virtualenv.

You can now install ``boto`` into this virtual environment::

    pip install boto

When you are done using ``boto``, you can deactivate your virtual environment::

    deactivate

If you are creating a lot of virtual environments, `virtualenvwrapper`_
is an excellent tool that lets you easily manage your virtual environments.

.. _`virtualenvwrapper`: http://virtualenvwrapper.readthedocs.org/en/latest/


Configuring Boto Credentials
----------------------------

You have a few options for configuring ``boto`` (see :doc:`boto_config_tut`).
For this tutorial, we'll be using a configuration file. First, create a
``~/.boto`` file with these contents::

    [Credentials]
    aws_access_key_id = YOURACCESSKEY
    aws_secret_access_key = YOURSECRETKEY

``boto`` supports a number of configuration values. For more information,
see :doc:`boto_config_tut`. The above file, however, is all we need for now.
You're now ready to use ``boto``.


Making Connections
------------------

``boto`` provides a number of convenience functions to simplify connecting to a
service. For example, to work with S3, you can run::

    >>> import boto
    >>> s3 = boto.connect_s3()

If you want to connect to a different region, you can import the service module
and use the ``connect_to_region`` functions. For example, to create an EC2
client in 'us-west-2' region, you'd run the following::

    >>> import boto.ec2
    >>> ec2 = boto.ec2.connect_to_region('us-west-2')


Troubleshooting Connections
---------------------------

When calling the various ``connect_*`` functions, you might run into an error
like this::

    >>> import boto
    >>> s3 = boto.connect_s3()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "boto/__init__.py", line 121, in connect_s3
        return S3Connection(aws_access_key_id, aws_secret_access_key, **kwargs)
      File "boto/s3/connection.py", line 171, in __init__
        validate_certs=validate_certs)
      File "boto/connection.py", line 548, in __init__
        host, config, self.provider, self._required_auth_capability())
      File "boto/auth.py", line 668, in get_auth_handler
        'Check your credentials' % (len(names), str(names)))
    boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials

This is because ``boto`` cannot find credentials to use. Verify that you have
created a ``~/.boto`` file as shown above. You can also turn on debug logging
to verify where your credentials are coming from::

    >>> import boto
    >>> boto.set_stream_logger('boto')
    >>> s3 = boto.connect_s3()
    2012-12-10 17:15:03,799 boto [DEBUG]:Using access key found in config file.
    2012-12-10 17:15:03,799 boto [DEBUG]:Using secret key found in config file.


Interacting with AWS Services
-----------------------------

Once you have a client for the specific service you want, there are methods on
that object that will invoke API operations for that service. The following
code demonstrates how to create a bucket and put an object in that bucket::

    >>> import boto
    >>> import time
    >>> s3 = boto.connect_s3()

    # Create a new bucket. Buckets must have a globally unique name (not just
    # unique to your account).
    >>> bucket = s3.create_bucket('boto-demo-%s' % int(time.time()))

    # Create a new key/value pair.
    >>> key = bucket.new_key('mykey')
    >>> key.set_contents_from_string("Hello World!")

    # Sleep to ensure the data is eventually there.
    >>> time.sleep(2)

    # Retrieve the contents of ``mykey``.
    >>> print key.get_contents_as_string()
    'Hello World!'

    # Delete the key.
    >>> key.delete()
    # Delete the bucket.
    >>> bucket.delete()

Each service supports a different set of commands. You'll want to refer to the
other guides & API references in this documentation, as well as referring to
the `official AWS API`_ documentation.

.. _`official AWS API`: https://aws.amazon.com/documentation/

Next Steps
----------

For many of the services that ``boto`` supports, there are tutorials as
well as detailed API documentation. If you are interested in a specific
service, the tutorial for the service is a good starting point. For instance,
if you'd like more information on S3, check out the :ref:`S3 Tutorial <s3_tut>`
and the :doc:`S3 API reference <ref/s3>`.