80
80
bucket so that when you call bucket.new_key() or when you get a listing
81
81
of keys in the bucket you will get an instances of your key class
82
82
rather than the default.
84
@type key_class: class
85
@param key_class: A subclass of Key that can be more specific
84
87
self.key_class = key_class
86
89
def lookup(self, key_name):
88
91
Deprecated: Please use get_key method.
93
@type key_name: string
94
@param key_name: The name of the key to retrieve
96
@rtype: L{Key<boto.s3.key.Key>}
97
@returns: A Key object from this bucket.
90
99
return self.get_key(key_name)
94
103
Check to see if a particular key exists within the bucket. This
95
104
method uses a HEAD request to check for the existance of the key.
96
105
Returns: An instance of a Key object or None
107
@type key_name: string
108
@param key_name: The name of the key to retrieve
110
@rtype: L{Key<boto.s3.key.Key>}
111
@returns: A Key object from this bucket.
98
113
response = self.connection.make_request('HEAD', self.name, key_name)
99
114
if response.status == 200:
125
140
there are no more results.
126
141
Called with no arguments, this will return an iterator object across
127
142
all keys within the bucket.
128
The prefix parameter allows you to limit the listing to a particular
129
prefix. For example, if you call the method with prefix='/foo/'
130
then the iterator will only cycle through the keys that begin with
132
The delimiter parameter can be used in conjunction with the prefix
133
to allow you to organize and browse your keys hierarchically. See:
134
http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
145
@param prefix: allows you to limit the listing to a particular
146
prefix. For example, if you call the method with prefix='/foo/'
147
then the iterator will only cycle through the keys that begin with
150
@type delimiter: string
151
@param delimiter: can be used in conjunction with the prefix
152
to allow you to organize and browse your keys hierarchically. See:
153
http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
156
@rtype: L{BucketListResultSet<boto.s3.bucketlistresultset.BucketListResultSet>}
157
@return: an instance of a BucketListResultSet that handles paging, etc
137
159
return BucketListResultSet(self, prefix, delimiter)
144
166
as defined in S3 Developer's Guide, however since max-keys is not
145
167
a legal variable in Python you have to pass maxkeys and this
146
168
method will munge it (Ugh!)
171
@param maxkeys: The maximum number of keys to retrieve
174
@param prefix: The prefix of the keys you want to retrieve
177
@param marker: The "marker" of where you are in the result set
179
@type delimiter: string
180
@param delimiter: "If this optional, Unicode string parameter is included with your request, then keys that contain the same string between the prefix and the first occurrence of the delimiter will be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response."
183
@return: The result from S3 listing the keys requested
149
187
for k,v in params.items():
170
208
raise S3ResponseError(response.status, response.reason, body)
172
210
def new_key(self, key_name=None):
214
@type key_name: string
215
@param key_name: The name of the key to create
217
@rtype: L{Key<boto.s3.key.Key>} or subclass
218
@returns: An instance of the newly created key object
173
220
return self.key_class(self, key_name)
175
222
def generate_url(self, expires_in, method='GET', headers=None):
176
223
return self.connection.generate_url(expires_in, method, self.name, headers=headers)
178
225
def delete_key(self, key_name):
227
Deletes a key from the bucket.
229
@type key_name: string
230
@param key_name: The key name to delete
179
232
response = self.connection.make_request('DELETE', self.name, key_name)
180
233
body = response.read()
181
234
if response.status != 204:
182
235
raise S3ResponseError(response.status, response.reason, body)
237
def copy_key(self, new_key_name, src_bucket_name, src_key_name, metadata=None):
239
Create a new key in the bucket by copying another existing key.
241
@type new_key_name: string
242
@param new_key_name: The name of the new key
244
@type src_bucket_name: string
245
@param src_bucket_name: The name of the source bucket
247
@type src_key_name: string
248
@param src_key_name: The name of the source key
251
@param metadata: Metadata to be associated with new key.
252
If metadata is supplied, it will replace the
253
metadata of the source key being copied.
254
If no metadata is supplied, the source key's
255
metadata will be copied to the new key.
257
@rtype: L{Key<boto.s3.key.Key>} or subclass
258
@returns: An instance of the newly created key object
261
headers = metadata.copy()
262
headers['x-amz-metadata-directive'] = 'REPLACE'
264
headers = {'x-amz-copy-source' : '%s/%s' % (src_bucket_name, src_key_name),
265
'x-amz-metadata-directive' : 'COPY'}
266
response = self.connection.make_request('PUT', self.name, new_key_name,
268
body = response.read()
269
if response.status == 200:
270
key = self.new_key(new_key_name)
271
h = handler.XmlHandler(key, self)
272
xml.sax.parseString(body, h)
273
if hasattr(key, 'Error'):
274
raise S3CopyError(key.Code, key.Message, body)
277
raise S3ResponseError(response.status, response.reason, body)
184
279
def set_canned_acl(self, acl_str, key_name=''):
185
280
assert acl_str in CannedACLStrings
186
281
response = self.connection.make_request('PUT', self.name, key_name,
223
318
raise S3ResponseError(response.status, response.reason, body)
320
def make_public(self, recursive=False):
321
self.set_canned_acl('public-read')
324
self.set_canned_acl('public-read', key.name)
225
326
def add_email_grant(self, permission, email_address, recursive=False):
227
328
Convenience method that provides a quick way to add an email grant to a bucket.
256
357
Convenience method that provides a quick way to add a canonical user grant to a bucket.
257
358
This method retrieves the current ACL, creates a new grant based on the parameters
258
359
passed in, adds that grant to the ACL and then PUT's the new ACL back to S3.
260
permission - The permission being granted. Should be one of:
261
READ|WRITE|READ_ACP|WRITE_ACP|FULL_CONTROL
262
See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingAuthAccess.html
263
for more details on permissions.
264
user_id - The canonical user id associated with the AWS account your are granting
266
recursive - A boolean value to controls whether the command will apply the
267
grant to all keys within the bucket or not. The default value is False.
268
By passing a True value, the call will iterate through all keys in the
269
bucket and apply the same grant to each key.
270
CAUTION: If you have a lot of keys, this could take a long time!
361
@type permission: string
362
@param permission: The permission being granted. Should be one of:
363
READ|WRITE|READ_ACP|WRITE_ACP|FULL_CONTROL
364
See http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingAuthAccess.html
365
for more details on permissions.
367
@type user_id: string
368
@param user_id: The canonical user id associated with the AWS account your are granting
371
@type recursive: bool
372
@param recursive: A boolean value that controls whether the command will apply the
373
grant to all keys within the bucket or not. The default value is False.
374
By passing a True value, the call will iterate through all keys in the
375
bucket and apply the same grant to each key.
376
CAUTION: If you have a lot of keys, this could take a long time!
274
378
if permission not in S3Permissions:
275
379
raise S3PermissionsError('Unknown Permission: %s' % permission)