def qiniuuploadfile(filepath, bucket_name, bucket_key=None):
    q = qiniu.Auth(ACCESS_KEY, SECRET_KEY)
    if not bucket_key:
        filetype = filepath.split('.')[-1]
        bucket_key = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ''.join(random.sample(string.ascii_lowercase, 6)) + filetype
    token = q.upload_token(bucket_name, bucket_key, 3600, policy={}, strict_policy=True)
    ret, info = put_file(token, bucket_key, filepath, version='v2')
    if info is not None:
        if info.status_code == 200:
            return True, getUrlWithBucketAndKey(bucket_name, ret["key"]),bucket_key
        else:
            return False, str(info),None
    else:
        return False,None,None
 def qiniuuploadfile(self, filepath, bucket_name, bucket_key):
     try:
         uploading = True
         retry_times = 1
         ret, info = None, None
         while uploading and retry_times <= 3:
             retry_times += 1
             token = q.upload_token(bucket_name, bucket_key, 3600, policy={}, strict_policy=True)
             ret, info = put_file(token, bucket_key, filepath, version='v2')
             if info is not None:
                 if info.status_code == 200:
                     return True, info.text_body
         return False, info
     except Exception:
         logexcption(msg='文件上传七牛服务器失败')
         return False, traceback.format_exc()
Beispiel #3
0
    def upload(self, names):
        '''

        Args:
            names:图片的media数组

        Returns:所有图片上传成功时,返回True,否则为False

        '''
        auth = AuthKeyHandler()
        #names = json.loads(names)
        bucket_name = 'shacus'
        q = Auth(auth.access_key, auth.secret_key)
        for name in names:
            token = q.upload_token(bucket_name, name, 345600)
            localfile = './{address}'.format(address=name)
            ret, info = put_file(token, name, localfile)
            if info.status_code != 200:
                return False
        return True
Beispiel #4
0
def qiniuuploadfile(filepath, bucket_name):
    q = qiniu.Auth(ACCESS_KEY, SECRET_KEY)
    filetype = filepath.split('.')[-1]
    key = "%s.%s" % (
        datetime.datetime.now().strftime('%Y%m%d%H%M%s') +
        ''.join(random.sample(string.ascii_lowercase, 5)), filetype)  # key 文件名
    print key
    token = q.upload_token(bucket_name,
                           key,
                           3600,
                           policy={},
                           strict_policy=True)
    ret, info = put_file(token, key, filepath)
    if info is not None:
        if info.status_code == 200:
            return True, "http://%s/%s" % (qiniu_url['file'], ret["key"]), key
        else:
            return False, str(info), None
    else:
        return False, None, None