def upload_assets(file_abs_path): """ 将文件放到百度云存储上面去,成功之后返回文件的URL地址,失败 返回None """ if not os.path.exists(file_abs_path): return None import pybcs import mimetypes pybcs.init_logging(logging.INFO) AK, SK, BUCKET, HOST = _get_bcs_instance() bcs = pybcs.BCS(HOST, AK, SK) b = bcs.bucket(BUCKET) file_sign = md5_sign(open(file_abs_path, 'rb').read()) file_ext = os.path.splitext(file_abs_path)[1] headers = { 'X-File-Name' : os.path.basename(file_abs_path) } content_type, _ = mimetypes.guess_type(file_abs_path) if content_type: headers['Content-Type'] = content_type o = b.object('/%s%s' % (file_sign, file_ext)) r = o.put_file(file_abs_path, headers) if r['status'] == 200: o.make_public() return '%s/%s/%s%s' % (HOST, BUCKET, file_sign, file_ext) return None
def upload_assets_raw(raw, file_ext): import pybcs import mimetypes pybcs.init_logging(logging.INFO) AK, SK, BUCKET, HOST = _get_bcs_instance() bcs = pybcs.BCS(HOST, AK, SK) b = bcs.bucket(BUCKET) content_type, _ = mimetypes.guess_type("HELLO%s" % file_ext) headers = {} if content_type: headers['Content-Type'] = content_type file_sign = md5_sign(raw) o = b.object('/%s%s' % (file_sign, file_ext)) r = o.put(raw, headers) if r['status'] == 200: o.make_public() return '%s/%s/%s%s' % (HOST, BUCKET, file_sign, file_ext) return None
def _init_logging(args): if not args.verbose : pybcs.init_logging(logging.WARN) logging.root.setLevel(logging.WARN) if args.verbose == 1: pybcs.init_logging(logging.INFO) logging.root.setLevel(logging.INFO) if args.verbose == 2: pybcs.init_logging(logging.DEBUG) logging.root.setLevel(logging.DEBUG) logging.info(args)
def _init_logging(args): if not args.verbose: pybcs.init_logging(logging.WARN) logging.root.setLevel(logging.WARN) if args.verbose == 1: pybcs.init_logging(logging.INFO) logging.root.setLevel(logging.INFO) if args.verbose == 2: pybcs.init_logging(logging.DEBUG) logging.root.setLevel(logging.DEBUG) logging.info(args)
#!/usr/bin/env python #coding:utf8 import os import logging import pybcs #设置日志级别 pybcs.init_logging(logging.INFO) # 请修改这里 AK = '7q6l1grGD7pYFoDG3pEKTxwE' #请改为你的AK SK = 'tpTQFy3lK6mKG1yBVGH3lBitwUsfmjhY' #请改为你的SK BUCKET = 'bc7q6l1grgd7pyfodg3pektxwembaas' bcs = pybcs.BCS('http://bcs.duapp.com/', AK, SK, pybcs.PyCurlHTTPC) #这里可以显式选择使用的HttpClient, 可以是: #HttplibHTTPC #PyCurlHTTPC lst = bcs.list_buckets() print '---------------- list of bucket : ' for b in lst: print b print '---------------- list end' #声明一个bucket b = bcs.bucket(BUCKET) #创建bucket (创建后需要在yun.baidu.com 手动调整quota, 否则无法上传下载) #b.create()
# coding:utf8 from django.core.files.storage import Storage from django.core import files import datetime, random, logging, os, os.path import StringIO logger = logging.getLogger("default") import pybcs pybcs.init_logging(logging.INFO) AK = "asaAuvEeFMOwyFMPcGe80uKz" SK = "EVX9MurfG2eGfTaBbp1bLCGwX4wK0D2G" BUCKET = "jianfei-baidu" bcs = pybcs.BCS("http://bcs.duapp.com/", AK, SK, pybcs.HttplibHTTPC) work_bucket = bcs.bucket(BUCKET) from rocket import settings try: from PIL import Image, ImageOps except ImportError: import Image import ImageOps class BaiduYunStorage(Storage): def _generate_name(self, name): dir_name, file_name = os.path.split(name) file_root, file_ext = os.path.splitext(file_name)
# coding:utf-8 # author : ning import os import sys, time import hashlib import random import unittest import logging sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + "/..") import pybcs # pybcs.init_logging(logging.ERROR, True, log_file_path=None) pybcs.init_logging(logging.INFO, True, log_file_path=None) # pybcs.init_logging(logging.DEBUG, True, log_file_path=None) HOST = "http://bcs.duapp.com" AK = os.environ["AK"] # 请改为你的AK SK = os.environ["SK"] # 请改为你的SK BUCKET = "pybcstest" USER = "******" ENABLE_LARGE = False common_header_list = set( [ "content-length",
#!/usr/bin/python #coding:utf-8 #author : ning import os import sys, time import hashlib import random import unittest import logging sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + "/..") import pybcs #pybcs.init_logging(logging.ERROR, True, log_file_path=None) pybcs.init_logging(logging.INFO, True, log_file_path=None) #pybcs.init_logging(logging.DEBUG, True, log_file_path=None) HOST = 'http://bs.baidu.com' AK = 'gNQ17x3aoMs8' SK = 'HWmtE17aWcg8sK2fn4B1J3w8vMm' BUCKET = 'p-59b805098a2c627624741539e70eb52e' OBJECT = '/009cf012ece59d79afbf3e73d9af739a' bcs1 = pybcs.BCS(HOST, AK, SK, pybcs.HttplibHTTPC) bcs2 = pybcs.BCS(HOST, AK, SK) for bcs in [bcs1, bcs2]: b = bcs.bucket(BUCKET) o = b.object(OBJECT) try: