def test_update_file_acl(self): scs = SCSBucket(self.bucket_name) try: scs.put_bucket('public-read-write') except: self.fail('create bucket:%s is failed'%self.bucket_name) content = u'this is a file content text!!' canned_acl = 'private' #快捷ACL scs.put(key=self.object_key, data=content, acl=canned_acl) #设置ACL acl = {} acl[ACL.ACL_GROUP_ANONYMOUSE] = [ACL.ACL_READ, ACL.ACL_WRITE, ACL.ACL_READ_ACP] acl[ACL.ACL_GROUP_CANONICAL] = [ACL.ACL_READ_ACP,ACL.ACL_WRITE_ACP, ACL.ACL_READ] user_id = 'SINA0000001001AABBCC' acl[user_id] = [ACL.ACL_WRITE, ACL.ACL_READ] scs.update_acl(self.object_key, acl) #assert file_aclInfo = scs.acl_info(self.object_key) #ACL_GROUP_ANONYMOUSE self.assertTrue(ACL.ACL_GROUP_ANONYMOUSE in file_aclInfo['ACL'], 'The acl dose not contains GRPS000000ANONYMOUSE group') self.assertTrue(ACL.ACL_READ in file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t read right') self.assertTrue(ACL.ACL_WRITE in file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t write right') self.assertTrue(ACL.ACL_READ_ACP in file_aclInfo['ACL'][ACL.ACL_GROUP_ANONYMOUSE], 'The acl GRPS000000ANONYMOUSE group hasn\'t read_acp right') #ACL_GROUP_CANONICAL self.assertTrue(ACL.ACL_GROUP_CANONICAL in file_aclInfo['ACL'], 'The acl dose not contains GRPS0000000CANONICAL group') self.assertTrue(ACL.ACL_READ in file_aclInfo['ACL'][ACL.ACL_GROUP_CANONICAL], 'The acl GRPS0000000CANONICAL group hasn\'t read right') self.assertTrue(ACL.ACL_WRITE_ACP in file_aclInfo['ACL'][ACL.ACL_GROUP_CANONICAL], 'The acl GRPS0000000CANONICAL group hasn\'t write_acp right') self.assertTrue(ACL.ACL_READ_ACP in file_aclInfo['ACL'][ACL.ACL_GROUP_CANONICAL], 'The acl GRPS0000000CANONICAL group hasn\'t read_acp right') #user_id self.assertTrue(user_id in file_aclInfo['ACL'], 'The acl dose not contains user_id group') self.assertTrue(ACL.ACL_READ in file_aclInfo['ACL'][user_id], 'The acl user_id group hasn\'t read right') self.assertTrue(ACL.ACL_WRITE in file_aclInfo['ACL'][user_id], 'The acl user_id group hasn\'t write right')
def update_file_acl(): s = SCSBucket('test11') acl = {} acl[ACL.ACL_GROUP_ANONYMOUSE] = [ACL.ACL_READ] acl[ACL.ACL_GROUP_CANONICAL] = [ACL.ACL_READ_ACP,ACL.ACL_WRITE_ACP] s.update_acl('sss.txt', acl)
def obj_upload(data, fileTag): """ Upload photo by object. """ sinastorage.setDefaultAppInfo(API_KEY, API_SECRET) s = SCSBucket('sharephotos') path = time.ctime().replace(' ', '_') # if '/' in file name there will be problems filename = fileTag[:6] + '_' + path + '.jpg' # use jpg temporary scs_response = s.put(filename, data) s.update_acl(filename, acl) url = s.make_url(filename) # get url of image in the storage return url
def url_upload(imgSrc='http://www.w3school.com.cn/i/site_photoref.jpg'): """ Upload photo by url. """ sinastorage.setDefaultAppInfo(API_KEY, API_SECRET) s = SCSBucket('sharephotos') data = urllib2.urlopen(imgSrc).read() path = imgSrc.split('/')[2] + '/%s__' % time.ctime() # if '/' in file name there will be problems filename = path + imgSrc.replace('/', '_') scs_response = s.put(filename, data) s.update_acl(filename, acl) url = s.make_url(filename) # get url of image in the storage return url
def storeImage(imgSrc): sinastorage.setDefaultAppInfo("1cjfyo5kQPdnsI3cUc6W", "a3c139370a3509f269331930515729747573aa10") djBucket = SCSBucket("dj-images") # not dj_images acl = {} # Access control list acl[ACL.ACL_GROUP_ANONYMOUSE] = [ACL.ACL_READ] data = urllib2.urlopen(imgSrc).read() path = imgSrc.split("/")[2] + "/" # if '/' in file name there will be problems filename = path + imgSrc.replace("/", "@") scsResponse = djBucket.put(filename, data) # upload the file stUrl = djBucket.make_url(filename) # get url of image in the storage djBucket.update_acl(filename, acl) # set acl for the file # save infomation to sql try: imgstorage.objects.get_or_create(original_url=imgSrc, storage_url=stUrl) except: print "insert into mysql error" pass # solve this later return stUrl
def run(self): try: self.mutex.lock() self.state = RunnableState.RUNNING s = SCSBucket(self.bucketName) scsResponse = s.update_acl(self.key, self.acl) self.response = scsResponse except SCSError, e: self.response = SCSResponse(e.urllib2Request, e.urllib2Response) self.response._responseBody = e.data self.state = RunnableState.DID_FAILED self.emitter.emit(QtCore.SIGNAL("UpdateFileACLDidFailed(PyQt_PyObject,PyQt_PyObject)"), self, e.msg) return
def run(self): try: self.mutex.lock() self.state = RunnableState.RUNNING s = SCSBucket(self.bucketName) scsResponse = s.update_acl(self.key, self.acl) self.response = scsResponse except SCSError, e: self.response = SCSResponse(e.urllib2Request, e.urllib2Response) self.response._responseBody = e.data self.state = RunnableState.DID_FAILED self.emitter.emit( QtCore.SIGNAL( "UpdateFileACLDidFailed(PyQt_PyObject,PyQt_PyObject)"), self, e.msg) return
class Uploader(): def __init__(self, host, accesskey, secretkey, bucket_name, remote_dir): self.host = host self.bucket_name = bucket_name self.remote_dir = remote_dir sinastorage.setDefaultAppInfo(accesskey, secretkey) self.handler = SCSBucket(self.bucket_name) def do(self, url, filepath_list, is_file=True): remote_list = {} sub_dir = self.get_subdir(url) if filepath_list: for _url, filepath, filename in filepath_list: if not filepath: continue filename = filename if filename else str(time.time()) + ".html" print("#INFO: uploader ", filename, sub_dir) sys.stdout.flush() try: remote_link = self.upload_file(filepath, filename, sub_dir, is_file) remote_list[_url] = remote_link except Exception as e: print("#ERR: upload filed failed, %s, %s", filename, str(e)) continue return remote_list @my_utils.time_limit(5) def upload_file(self, local_path, local_name, sub_dir="", is_file=True): if not local_path or not local_name: return "" if sub_dir: sub_dir = sub_dir + "/" remote_path = self.remote_dir + "/" + sub_dir + local_name ret = self._upload_file(remote_path, local_path, is_file) self.set_acl(remote_path) return ret def _upload_file(self, remote_path, local_path, is_file=True): if is_file: ret = self.handler.putFile(remote_path, local_path) else: ret = self.handler.put(remote_path, local_path) return self.host + "/" + self.bucket_name + "/" + remote_path if ret else "" def set_acl(self, remote_path): acl = {} acl[ACL.ACL_GROUP_ANONYMOUSE] = [ACL.ACL_READ] acl[ACL.ACL_GROUP_CANONICAL] = [ACL.ACL_READ_ACP, ACL.ACL_WRITE_ACP] self.handler.update_acl(remote_path, acl) def get_subdir(self, url): url_p = urlparse.urlparse(url) return url_p.hostname.replace(".", "_")