cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
s3object.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 import hashlib
21 import os
22 from ec2.base.s3action import S3Action
23 from ec2.settings import BUCKETS_PATH
24 from ec2.error import ResourceLimitExceeded, EntityTooLarge, IncompleteBody
25 from ec2.helpers.auth import authenticate
26 from ec2.helpers.utils import check_user_quota
27 
28 ##
29 # @package src.s3object
30 # S3 base actions for objects
31 #
32 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
33 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
34 #
35 
36 BLOCK_SIZE = 65536
37 
38 class PutObject(S3Action):
39  def _execute(self):
40  print 'PutObject'
41  try:
42  size = int(self.parameters['content_length'])
43  if not check_user_quota(self.user_name, size):
44  # not quite an S3 exception, but original S3 doesn't run out of space
45  raise EntityTooLarge
46 
47  new_file = open(self.path, 'wb')
48 
49  real_size = 0
50  m = hashlib.md5()
51  while True:
52  chunk = self.parameters['input'].read(128)
53  real_size += len(chunk)
54 
55  if not chunk:
56  break
57  if real_size > size:
58  raise IncompleteBody
59 
60  m.update(chunk)
61  new_file.write(chunk)
62 
63  new_file.close()
64  response = {'headers': [('ETag', '"' + m.hexdigest() + '"')], 'body': ''}
65 
66  return response
67  except Exception, error:
68  raise error
69 
70 class GetObject(S3Action):
71  def _execute(self):
72 
73  image_file = open(self.path, 'r')
74 
75  if 'File_wrapper' in self.parameters:
76  return {'body': self.parameters['File_wrapper'](image_file, BLOCK_SIZE)}
77  else:
78  return {'body': iter(lambda: image_file.read(BLOCK_SIZE), '')}
79