def __init__(self): self.auth = oss2.Auth(Config.oss_access_key_id, Config.oss_access_key_secret) self.service = oss2.Service(self.auth, Config.oss_backet)
def __get_bucket(b_name): auth = oss2.Auth(access_key_id, access_key_secret) return oss2.Bucket(auth, endpoint, b_name)
print(count) #删除本地的jpg和svg文件 # import cv2 as cv2 # print("绘制完成,请检查") else: print (num) if __name__ == '__main__': # print("start \n"); # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 auth = oss2.Auth(accesskey_id,accesskey_secret) # Endpoint以杭州为例,其它Region请按实际情况填写。 bucket = oss2.Bucket(auth,endpoint , bucket_name); bucket_svg = oss2.Bucket(auth,endpoint , bucket_name_svg); #单个文件夹下载 #prefix_all_list(bucket, "newDown"); root_directory_list(bucket,bucket_svg); # print("end \n");
import os import oss2 import requests from bs4 import BeautifulSoup from django.http import HttpResponse import shutil import json access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'Your ACCESS_KEY_ID') access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', 'Your ACCESS_KEY_SECRET') endpoint = os.getenv('OSS_TEST_ENDPOINT', 'http://oss-cn-beijing.aliyuncs.com') path = os.path.abspath('.') dir_path = os.path.abspath('static/media') dirs = os.listdir(path) bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name="maya-music") now_pwd = os.getcwd() def index(request): base_url = "https://maya-music.oss-cn-beijing.aliyuncs.com/" files_name = [] url_list = [] data = [] # 打印出云端每个文件的名字,将名字存入files_name列表 for i in oss2.ObjectIterator(bucket): files_name.append(i.key[:-4]) # 拼接出所有链接,存入url_list字典 for name in files_name:
import oss2 auth = oss2.Auth('25704006', 'ab8c9ccc96d3621e529d18545d8ff65d') # Access key ID and Access key secret bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'SIOT') # Endpoint url based on location and name of storage space. bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE) # Set storage space as private
#_*_coding:utf-8_*_ # check the python SDK for more explanations if needed # file management of aliyun's Object Storage Service import oss2 from itertools import islice from os import walk # connect endpoint = 'http://oss-cn-shenzhen.aliyuncs.com' auth = oss2.Auth('Rfh4U5ZcodMCHIDV', 'mtWkRREAzh0T66kr7E7rMYv4J4xfxI') service = oss2.Service(auth, endpoint, connect_timeout=60000) #print( [b.name for b in oss2.BucketIterator(service)] ) # select bucket bucket = oss2.Bucket(auth, endpoint, 'ou-ms-software') #bucket = oss2.Bucket(auth, endpoint, 'ou-image') #bucket = oss2.Bucket(auth, endpoint, 'pcom-data') # create bucket #bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE) # upload file file_dir = '/home/ou/temp/' (_, _, file_names) = walk(file_dir).next() #bucket.put_object_from_file('scan_id.zip','/Users/ou/temp/scan_id.zip') for filename in file_names: remote_name = filename local_name = file_dir + filename bucket.put_object_from_file(remote_name, local_name)
def __init__(self, access_key=app.config['ALIYUN_ACCESS_KEY'], secret_key=app.config['ALIYUN_SECRET_KEY'], endpoint=ENDPOINT): self.endpoint = endpoint self.auth = oss2.Auth(access_key, secret_key)
start_time = time.time() # 校验输入 if JobType not in ['LOCAL_TO_S3', 'S3_TO_S3', 'ALIOSS_TO_S3']: logger.warning('ERR JobType, check config file') sys.exit(0) # 定义 s3 client s3_config = Config(max_pool_connections=25) s3_dest_client = Session(profile_name=DesProfileName).client( 's3', config=s3_config) if JobType == 'S3_TO_S3': s3_src_client = Session(profile_name=SrcProfileName).client( 's3', config=s3_config) elif JobType == 'ALIOSS_TO_S3': ali_bucket = oss2.Bucket( oss2.Auth(ali_access_key_id, ali_access_key_secret), ali_endpoint, ali_SrcBucket) # 检查目标S3能否写入 try: logger.info('Checking write permission for dest. S3 bucket') s3_dest_client.put_object(Bucket=DesBucket, Key=str( PurePosixPath(S3Prefix) / 'access_test'), Body='access_test_content') except Exception as e: logger.error('Can not write to dest. bucket/prefix. ERR: ' + str(e)) sys.exit(0) # 获取源文件列表 logger.info('Get source file list')
import oss2 import requests import random from celery.utils.log import get_task_logger from tasks.application import app from scripture.settings import USER_AGENT_LIST from tasks import settings logger = get_task_logger('tasks') endpoint = settings.OSS_ENDPOINT auth = oss2.Auth(settings.OSS_ACCESS_KEY_ID, settings.OSS_SECRET_ACCESS_KEY) bucket = oss2.Bucket(auth, endpoint, settings.OSS_BUCKET) @app.task(autoretry_for=(Exception, ), retry_kwargs={'max_retries': 2}) def _upload_image(url, path): user_agent = random.choice(USER_AGENT_LIST) resp = requests.get(str(url), headers={'User-Agent': user_agent}) logger.info('Image(%s), StatusCode(%d)', url, resp.status_code) if resp.status_code != 200: logger.error( f'download image fail ,Image({url}), StatusCode({resp.status_code})' ) return False put_result = bucket.put_object(path, resp.content) logger.info( f'Fetched object {path} source {url} status {put_result.status}') if put_result.status != 200: logger.error( f'upload image fail ,Image({path}), StatusCode({put_result.status})'
def to_excel(self, request, queryset): # new_id_list = [] # for qs in queryset: # xg_content = qs.XGcontext if qs.XGcontext else '' # if not xg_content: # new_id_list.append(str(int(qs.id))) # if new_id_list: # new_id_str = ','.join(new_id_list) # messages.error(request, '您当前使用的“旧版导出Excel”功能,下列ID:%s 信息修改为新版申请记录,请剔除后再使用旧功能导出!!!' % new_id_str) # else: # details = eval(qs.Details) if qs.Details else {} # if not details: path = MEDIA_ROOT + 'download_xls/' + request.user.username mkdir_p(MEDIA_ROOT + 'download_xls') os.popen('chmod 777 %s' % (MEDIA_ROOT + 'download_xls')) mkdir_p(path) os.popen('chmod 777 %s' % (path)) w = Workbook() sheet = w.add_sheet('information_modify') sheet.write(0, 0, u'id') sheet.write(0, 1, u'商品编码') sheet.write(0, 2, u'商品名称') sheet.write(0, 3, u'英文Keywords') sheet.write(0, 4, u'中文Keywords') sheet.write(0, 5, u'申请人') sheet.write(0, 6, u'申请时间') sheet.write(0, 7, u'修改类型') sheet.write(0, 8, u'修改描述') sheet.write(0, 9, u'旧值') sheet.write(0, 10, u'新值') sheet.write(0, 11, u'一部领用人') sheet.write(0, 12, u'一部领用日期') sheet.write(0, 13, u'一部领用状态') sheet.write(0, 14, u'二部领用人') sheet.write(0, 15, u'二部领用日期') sheet.write(0, 16, u'二部领用状态') sheet.write(0, 17, u'三部领用人') sheet.write(0, 18, u'三部领用日期') sheet.write(0, 19, u'三部领用状态') sheet.write(0, 20, u'四部领用人') sheet.write(0, 21, u'四部领用日期') sheet.write(0, 22, u'四部领用状态') sheet.write(0, 23, u'五部领用人') sheet.write(0, 24, u'五部领用日期') sheet.write(0, 25, u'五部领用状态') new_id_list = [] # 写数据 row = 0 for qs in queryset: details = eval(qs.Details) if qs.Details else {} if details: new_id_list.append(str(int(qs.id))) continue InputBox_list_s = [] if qs.InputBox is not None: InputBox_list_s = qs.InputBox.split(',') num = 0 qs_id = '' for InputBox_list_l in InputBox_list_s: if InputBox_list_l.strip() != '': row = row + 1 column = 0 sheet.write(row, column, qs.id) column = column + 1 sheet.write(row, column, InputBox_list_l) column = column + 1 sheet.write(row, column, qs.Name2) column = column + 1 sheet.write(row, column, qs.Keywords) column = column + 1 sheet.write(row, column, qs.Keywords2) column = column + 1 sheet.write(row, column, qs.SQStaffNameing) column = column + 1 rt = '' rt = u'%s%s' % (rt, qs.SQTimeing) sheet.write(row, column, u'%s' % rt) column = column + 1 sheet.write(row, column, TYPE_DICT.get(qs.Select, '')) column = column + 1 sheet.write(row, column, qs.XGcontext) if qs.newvalue and qs.oldvalue: oldvalues = qs.oldvalue.split(',') newvalues = qs.newvalue.split(',') if not qs_id or not qs_id == qs.id: qs_id = qs.id num = 0 oldvalue = oldvalues[num] newvalue = newvalues[num] num += 1 else: try: oldvalue = oldvalues[num] except: oldvalue = '' try: newvalue = newvalues[num] except: newvalue = '' num += 1 else: oldvalue = '' newvalue = '' column = column + 1 sheet.write(row, column, oldvalue) column = column + 1 sheet.write(row, column, newvalue) column = column + 1 sheet.write(row, column, qs.Dep1) column = column + 1 sheet.write(row, column, u'%s' % qs.Dep1Date) column = column + 1 sheet.write(row, column, qs.Dep1Sta) column = column + 1 sheet.write(row, column, qs.Dep2) column = column + 1 sheet.write(row, column, u'%s' % qs.Dep2Date) column = column + 1 sheet.write(row, column, qs.Dep2Sta) column = column + 1 sheet.write(row, column, qs.Dep3) column = column + 1 sheet.write(row, column, u'%s' % qs.Dep3Date) column = column + 1 sheet.write(row, column, qs.Dep3Sta) column = column + 1 sheet.write(row, column, qs.Dep4) column = column + 1 sheet.write(row, column, u'%s' % qs.Dep4Date) column = column + 1 sheet.write(row, column, qs.Dep4Sta) column = column + 1 sheet.write(row, column, qs.Dep5) column = column + 1 sheet.write(row, column, u'%s' % qs.Dep5Date) column = column + 1 sheet.write(row, column, qs.Dep5Sta) filename = request.user.username + '_' + datetime.now().strftime('%Y%m%d%H%M%S') + '.xls' w.save(path + '/' + filename) os.popen(r'chmod 777 %s' % (path + '/' + filename)) # 上传oss对象 auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET) bucket = oss2.Bucket(auth, ENDPOINT, BUCKETNAME_XLS) bucket.create_bucket(oss2.BUCKET_ACL_PUBLIC_READ) # 删除现有的 for object_info in oss2.ObjectIterator(bucket, prefix='%s/%s_' % (request.user.username, request.user.username)): bucket.delete_object(object_info.key) bucket.put_object(u'%s/%s' % (request.user.username, filename), open(path + '/' + filename)) messages.error(request, u'%s%s.%s/%s/%s' % (PREFIX, BUCKETNAME_XLS, ENDPOINT_OUT, request.user.username, filename) + u':成功导出,可点击Download下载到本地............................。') if new_id_list: new_id_str = ','.join(new_id_list) messages.error(request, '您当前使用的“旧版导出Excel”功能,下列ID:%s 信息修改为新版申请记录,已为您剔除。如果您仍想要导出这部分,请选择“导出Excel(新版)”导出这部分' % new_id_str)
def _auth(self): auth = oss2.Auth(self._ak_id, self._ak_secret) return auth
def download_war(object, version, run_args, redis_key): #下载对应项目的最新代码包 try: #包名需要规范 Files = tools.get_k8s_packages() project_file = object object = object.split('.') dm_name = object[0] if dm_name in Files: project_file = Files[dm_name] dm_type = project_file.split('.')[-1] if len(project_file.split('.')) > 2: dm_type = '.'.join(project_file.split('.')[1:]) package = '%s-%s.%s' % (dm_name, version, dm_type) project_path = '%s/%s/%s' % (dockerfile_path, dm_name, project_file) if not os.path.exists(project_path): try: Redis.lpush(redis_key, '%s package download from oss ......' % package) _flow_log('%s package download from oss ......' % package) auth = oss2.Auth(oss_id, oss_key) bucket = oss2.Bucket(auth, oss_url, 'mojiops') oss_project_path = None try: if not os.path.exists('%s/%s' % (dockerfile_path, dm_name)): os.mkdir('%s/%s' % (dockerfile_path, dm_name)) for obj in oss2.ObjectIterator(bucket): if obj.key.endswith('.war') or obj.key.endswith( '.tar.gz') or obj.key.endswith('.jar'): if obj.key.split('/')[-1].startswith( dm_name) and version in obj.key.split( '/')[-1]: oss_project_path = obj.key break except Exception as e: logging.error(e) if oss_project_path: #尝试3次下载 for i in range(3): try: oss2.resumable_download(bucket, oss_project_path, project_path) break except: continue else: Redis.lpush(redis_key, '%s package not fond!' % package) _flow_log('%s package not fond!' % package) return False except Exception as e: logging.error(e) if os.path.exists(project_path): try: if project_file.endswith('.tar.gz'): project_file = project_file.split('.')[0] os.chdir('%s/%s/' % (dockerfile_path, dm_name)) tar = tarfile.open(project_path.split('/')[-1], 'r') tar.extractall() tar.close() for file in os.listdir('./'): if dm_name in file and not file.endswith('.tar.gz'): shutil.move(file, project_file) if os.path.exists(dm_name): os.remove(project_path.split('/')[-1]) #生成dockerfile文件 dockerfile = '%s/%s/Dockerfile' % (dockerfile_path, dm_name) if os.path.exists(dockerfile): os.remove(dockerfile) with open(dockerfile, 'a+') as f: with open('%s/../conf/dockerfile_%s.template' % (app.root_path, dm_type)) as F: for line in F: if '<PROJECT>' in line: line = line.replace('<PROJECT>', project_file) f.write('%s\n' % line) #生成docker_run启动脚本文件 if run_args: runfile = '%s/%s/run.sh' % (dockerfile_path, dm_name) if os.path.exists(runfile): os.remove(runfile) with open(runfile, 'a+') as f: with open('%s/../conf/docker_run.template' % app.root_path) as F: for line in F: f.write('%s\n' % line) for line in run_args: f.write('%s\n' % line) Redis.lpush(redis_key, '%s package download success!' % package) _flow_log('%s package download success!' % package) return package except Exception as e: logging.error(e) else: Redis.lpush(redis_key, '%s package download fail!' % package) _flow_log('%s package download fail!' % package) return False except Exception as e: logging.error(e)
import base64 import hashlib import os import oss2 import requests auth = oss2.Auth('AccessKeyId', 'AccessKeySecret') bucket = oss2.Bucket(auth, 'Endpoint', 'BucketName') oss_public_domain = 'OssDomain' local_dir = "/Users/Poison/blog/public/" def content_md5(local_file_path): with open(local_file_path, 'rb') as fileobj: m = hashlib.md5() while True: d = fileobj.read(8096) if not d: break m.update(d) return str(base64.b64encode(m.digest())) def upload_file_to_aliyun_oss(local_file_path): if local_file_path.endswith(".DS_Store"): return oss_object_key = local_file_path[local_dir.__len__():] local_file_md5 = content_md5(local_file_path) exist = bucket.object_exists(oss_object_key)
def __init__(self): self.auth = oss2.Auth(os.environ.get('access_key_id'), os.environ.get('access_key_secret')) self.bucket = oss2.Bucket(self.auth, os.environ.get('oss_endpoint'), os.environ.get('bucket_name'))
import os import oss2 import requests from bs4 import BeautifulSoup from django.http import HttpResponse import shutil import json from pprint import pprint access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', 'LTAIwmIDMn3td8tv') access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '7KVaHZZQdOqVsiaftwWOBvLYL2sHaM') endpoint = os.getenv('OSS_TEST_ENDPOINT', 'http://oss-cn-beijing.aliyuncs.com') path = os.path.abspath('.') dir_path = os.path.abspath('static/media') dirs = os.listdir(path) bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name="maya-music") def index(request): base_url = "https://maya-music.oss-cn-beijing.aliyuncs.com/" files_name = [] url_list = [] data = [] # 打印出云端每个文件的名字,将名字存入files_name列表 for i in oss2.ObjectIterator(bucket): files_name.append(i.key[:-4]) # 拼接出所有链接,存入url_list字典 for name in files_name: url_list.append(base_url + name + '.mp3') for song, src in zip(files_name, url_list): data.append({'song': song, 'src': src, 'singer': "黄耀明", 'img': 'static/css/img/hym.jpg', 'lyric': 'anyong'})
#!/usr/bin/python3 # -*- coding:utf-8 -*- import oss2 import crcmod._crcfunext # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 auth = oss2.Auth('<LTAIUq021PIVpJzU>', '<Heir5KmrxttWrYxFkeL41f6ZOr1h4k>') # Endpoint以杭州为例,其它Region请按实际情况填写。 bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<test_python>') # 设置存储空间为私有读写权限。 bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE)
import logging import random file = open('config.json', 'r', encoding='utf-8') config = json.load(file) aws_session = boto3.session.Session( aws_access_key_id=config['aws_access_key_id'], aws_secret_access_key=config['aws_secret_access_key']) #client = aws_session.client('sqs') sqs = aws_session.resource('sqs') auth = oss2.Auth('', '') firstbucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'seimutig') msg = [] for obj in oss2.ObjectIterator(firstbucket): c = obj.key if c[-1:] == '/': #os.mkdir("/Volumes/4T/mnt/"+ c) pass else: msg.append(c) # firstbucket.get_object_to_file(c,"/mnt/"+c)
#以下可以不修改 path_oss_store= "oneindex-serverless"#在存储桶中创建的文件夹名称 filename_token= "token.json"#保存凭据的文件名 items_per_page=50#每次获取的项目数量 #以下请勿更改 app = Flask(__name__) app.secret_key = 'oneindex-serverless' client_id = '0375f766-e528-4748-91e2-7d8fcb702889' client_secret = 'vXOJL93{#?xnotilNIU895:' redirect_uri_register = 'https://oneindex-serverless.github.io/redirect' redirect_uri_final = '{}/login/authorized'.format(url_host.strip("/")) auth = oss2.Auth(access_key,access_key_secret) bucket = oss2.Bucket(auth, oss_end_point, oss_bucket_name) base_url='https://graph.microsoft.com/v1.0/' scopes= "offline_access files.readwrite.all" select="id,name,size,folder,image,video,lastModifiedDateTime" token=None oss_available=False def initializer(context): init() def handler(environ, start_response): return app(environ, start_response) @app.route('/')
# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。 # 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。 access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '<你的AccessKeyId>') access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '<你的AccessKeySecret>') bucket_name = os.getenv('OSS_TEST_BUCKET', '<你的Bucket>') endpoint = os.getenv('OSS_TEST_ENDPOINT', '<你的访问域名>') # 确认上面的参数都填写正确了 for param in (access_key_id, access_key_secret, bucket_name, endpoint): assert '<' not in param, '请设置参数:' + param # 列举所有的Bucket # 1. 先创建一个Service对象 # 2. 用oss2.BucketIterator遍历 service = oss2.Service(oss2.Auth(access_key_id, access_key_secret), endpoint) print('\n'.join(info.name for info in oss2.BucketIterator(service))) # 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行 bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name) # 下面只展示如何配置静态网站托管。其他的Bucket操作方式类似,可以参考tests/test_bucket.py里的内容 # 方法一:可以生成一个BucketWebsite对象来设置 bucket.put_bucket_website(oss2.models.BucketWebsite('index.html', 'error.html')) # 方法二:可以直接设置XML xml = ''' <WebsiteConfiguration>
# -*- coding: utf-8 -*- import oss2 import hashlib import os import sys from common.utils import clientId, clientSecret, calc_md5 from common.log_util import logger auth = oss2.Auth(clientId, clientSecret) endpoint_cn = 'oss-cn-shanghai.aliyuncs.com' bucket_name_cn = 'studio88images' bucket_cn = oss2.Bucket(auth, endpoint_cn, bucket_name_cn) # oss related config OSS_ANIMATED_STICKER_DIR = "asset/gif/" OSS_STATIC_STICKER_DIR = "asset/jpg/" def get_oss_path(file_path): fmd5 = calc_md5(file_path) if file_path.split('.')[-1].upper() == 'GIF': oss_path = OSS_ANIMATED_STICKER_DIR + fmd5 + '.gif' else: oss_path = OSS_STATIC_STICKER_DIR + fmd5 + '.jpg' return oss_path def get_url(name, type='cn'):
@xadmin.sites.register(views.CommAdminView) class GlobalSetting(object): global_search_models = [Page] site_title = "切分后台管理系统" site_footer = "@longquan" menu_style = 'default' # 'accordion' @xadmin.sites.register(Page) class PageAdmin(object): list_display = Page.Config.list_display_fields list_display_links = ("id") search_fields = Page.Config.search_fields auth = oss2.Auth(os.environ.get('OSS_API_KEY', 'key'), os.environ.get('OSS_API_SECRET', 'pass')) bucket = oss2.Bucket(auth, 'oss-cn-shanghai.aliyuncs.com', 'tripitaka') def is_img_exist(image_name): return bucket.object_exists(get_oss_by_name(image_name)) def put_zip_into_db(batch_version, zip_path): img_not_exist = [] opage_list = [] page_list = [] data_list = [] zfile = zipfile.ZipFile(zip_path, 'r') # TODO 要和用户约定上传文件格式 for i in zfile.namelist():
def upload(filename, content): auth = oss2.Auth(OSS_CONF['key'], OSS_CONF['access_key']) bucket = oss2.Bucket(auth, 'oss-cn-shanghai.aliyuncs.com', 'ltfreddeblog') bucket.put_object(filename, content) return 'https://ltfreddeblog.oss-cn-shanghai.aliyuncs.com/' + filename
def sku_property_list(objid): logger = logging.getLogger( 'sourceDns.webdns.views') #刚才在setting.py中配置的logger #header name t_product_develop_ing_obj = models.t_product_develop_ing.objects.get( id__exact=objid) mainsku_arrt_names = models.t_product_mainsku_arrt_name.objects.filter( MainSKU=t_product_develop_ing_obj.MainSKU).order_by('Attrid') mainsku_skus = models.t_product_sku_attr_value.objects.filter( MainSKU=t_product_develop_ing_obj.MainSKU).order_by('SKU') sku_attr_values = {} tmp_sku_pics = {} for mainsku_sku in mainsku_skus: logger.error('mainsku_sku.SKU=%s' % mainsku_sku.SKU) sku_attr_values[ mainsku_sku.SKU] = models.t_product_sku_attr_value.objects.filter( MainSKU=t_product_develop_ing_obj.MainSKU, SKU=mainsku_sku.SKU).order_by('Attrid') ##tmp_sku_pics[mainsku_sku.SKU] = '%s'%(t_product_develop_ing_obj.MainSKU) t_product_mainsku_sku_objs = models.t_product_mainsku_sku.objects.filter( MainSKU=t_product_develop_ing_obj.MainSKU, pid=objid) for t_product_mainsku_sku_obj in t_product_mainsku_sku_objs: tmp_sku_pics[t_product_mainsku_sku_obj.SKU] = '%s' % ( t_product_develop_ing_obj.MainSKU) logger.error("t_product_mainsku_sku_objs = %s " % t_product_mainsku_sku_objs) logger.error("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") logger.error('mainsku_arrt_names=%s' % mainsku_arrt_names) logger.error('mainsku_skus=%s' % mainsku_skus) logger.error('sku_attr_values=%s' % sku_attr_values) i = 0 attrijs = {} for (key, value) in sku_attr_values.items(): j = 0 xxj = {} for mainsku_arrt_name in mainsku_arrt_names: xxj['j%d' % j] = '' for v in value: if v.Attrid == mainsku_arrt_name.Attrid: logger.error(u'%d %d = %s' % (i, j, v.Attrid)) xxj['j%d' % j] = v.AttrValue break j = j + 1 logger.error(u'xxj====== %s' % xxj) attrijs[key] = xxj i = i + 1 logger.error(u'attrijs====== %s' % attrijs) #获取子SKU的图片路径 auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET) sku_pics = {} for (key, value) in tmp_sku_pics.items(): bucket = oss2.Bucket(auth, ENDPOINT, BUCKETNAME_DEV) pictures = [] for object_info in oss2.ObjectIterator(bucket, prefix='%s/%s/' % (value, key)): pictures.append( u'%s%s.%s/%s' % (PREFIX, BUCKETNAME_DEV, ENDPOINT_OUT, object_info.key)) sku_pics[key] = pictures return { 'mainsku_arrt_names': mainsku_arrt_names, 'attrijs': attrijs, 'sku_pics': sku_pics }
async def post(self, *args, **kwargs): res = RestResponseMsg() try: # data = self.request.body.decode('utf-8') if self.request.body else "{}" # print(self.request.files.keys()) # print(len(self.request.files.get('username'))) # print(self.request.files.get('username')[0].keys()) upload_host_url_list = [] for key in self.request.files: # print(self.request.files.get(key)[0].keys()) # print(len(self.request.files.get(key)[0]['body']) / 1024 / 1024) new_file_name = ''.join(str(uuid.uuid1()).split('-')) file_name = self.request.files.get(key)[0]['filename'] file_size = len(self.request.files.get(key)[0]['body']) file_content = self.request.files.get(key)[0]['body'] check_name = file_name.split('.')[-1] if check_name.lower() not in FILE_CHECK: res.update(message=file_name + '不是规定的文件类型(%s)!' % '/'.join(FILE_CHECK), errorCode=2) if file_size > FILE_SIZE: res.update(messge=file_name + '文件超过64mb,无法上传。', errorCode=2) save_file_name = new_file_name + '.' + check_name if UPLOAD_FILE_LOCATION == 'local': upfile_base_dir = os.path.join( os.path.dirname( os.path.dirname( os.path.dirname(os.path.abspath(__file__)))), 'static/files/') now_file_path = os.path.join(upfile_base_dir, str(datetime.now().date())) is_have = os.path.exists(now_file_path) if is_have: save_path = os.path.join(now_file_path, save_file_name) else: os.makedirs(now_file_path) save_path = os.path.join(now_file_path, save_file_name) with open(save_path, 'wb') as u_file: u_file.write(file_content) host_file_url = SERVER_NAME + '/files/' + save_file_name upload_host_url_list.append(host_file_url) else: # 直接上传到oss auth = oss2.Auth(AliOSS_ACCESS_KEY_ID, AliOSS_ACCESS_KEY_SECRET) bucket = oss2.Bucket(auth, AliOSS_END_POINT, AliOSS_BUCKET_NAME) result = bucket.put_object(save_file_name, file_content) if result.status != 200: print(result) print((dir(result))) res.update(message="上传出现异常", errorCode=1) self.finish(res.data) return res.data url = bucket.sign_url('GET', file_name, 60) upload_host_url_list.append(unquote(url.split('?')[0])) res.update( data=upload_host_url_list if upload_host_url_list else {}) self.finish(res.data) return res.data except ValidationError as err: res.update(message=str(err.messages), errorCode=2) self.finish(res.data) return res.data except Exception as e: logging.error('出现异常:%s' % str(e)) res.update(message="出现无法预料的异常:{}".format(str(e)), errorCode=1) self.finish(res.data) return res.data
import oss2 # 配置OSS auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret') bucket = oss2.Bucket(auth, 'http://oss-cn-shenzhen.aliyuncs.com', 'yourBucketName') def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) method = environ['REQUEST_METHOD'] path = environ['PATH_INFO'] if method == 'GET' and path == '/': imgUrl = bucket.sign_url('GET', 'raspiInteractiveCameraDemo/test.jpg', 6000) # print("The url of that image is :" + imgUrl) return_url = "<img src=\"" + imgUrl + "\" />" return_url = bytes(return_url, encoding="utf8") # print(return_url) return_list = ["", ""] return_list[0] = [b'hello'] return_list[1] = [return_url] return return_list[1] from wsgiref.simple_server import make_server # 创建一个服务器,IP地址为空,端口是8999 httpd = make_server('', 8999, application) print('Serving HTTP on port 8000...') # 开始监听HTTP请求:
class PicFillPlugin(Plugin): name = "picFillPlugin" version = '0.0.1' HOST = '52a34c024547489a.m.cnbja.kvstore.aliyuncs.com' DB = 12 PORT = 6379 PASSWD='52a34c024547489a:0s9j09sHSj1sdf1oL' ID_GEN = 'zhanqun_id_generator' ID_RECORD = 'zhanqun_taskid_record' img_url_rec = 'zhanqun_pic_rec' client = redis.StrictRedis(host=HOST, port=PORT, db=DB, password=PASSWD) auth = oss2.Auth('BPvWuBAlq5rxM3qm', '1EMB2SelO9EQaue3E3xN09zJajB4Dm') bucket = oss2.Bucket(auth, 'oss-cn-beijing.aliyuncs.com', 'genshuixue-public') pic_fill_conf = {} pic_prefix = "http://img.gsxservice.com/zhanqun/" def __init__(self): Plugin.__init__(self) def scan(self, config={}): return "picFill plugin" def processInit(self): try: for line in open('conf/pic_fill.conf'): line = line.strip() if line.startswith('#'): continue else: f = line.split('\t') cls = f[0] attr = f[1] way = f[2] if self.pic_fill_conf.has_key(cls): self.pic_fill_conf[cls][attr] = way else: self.pic_fill_conf[cls] = {} self.pic_fill_conf[cls][attr] = way except: logger.error('load pic_fill.conf exception.') return -1 return 0 def packetInit(self): return 0 def generateFileName(self, imgUrl): m2 = hashlib.md5() m2.update(imgUrl) return m2.hexdigest() def uploadImgFile(self, fileName): try: full_file_name = './data/pics/' + fileName with open(full_file_name, 'rb') as fileobj: self.bucket.put_object('zhanqun/' + fileName , fileobj) return 0 except: return 1 def getAndSaveImg(self, imgUrl): if (len(imgUrl) != 0): if self.client.hexists(self.img_url_rec, imgUrl) == 1: return self.client.hget(self.img_url_rec, imgUrl) imgUrl = imgUrl.strip() f = imgUrl.split('.') suffix = f[-1] if len(suffix) > 5: suffix = 'jpg' url_md5 = self.generateFileName(imgUrl) fileName = url_md5 + '.' + suffix try: user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' request = urllib2.Request(imgUrl) request.add_header('User-agent', user_agent) response = urllib2.urlopen(request) ret_code = response.getcode() if ret_code != 200: logger.debug('download img with method1 fail:url:%s, new_name:%s' %(imgUrl, fileName)) return None data = response.read() full_file_name = './data/pics/' + fileName img_file = file(full_file_name, "wb") img_file.write(data) img_file.close() logger.debug('download img succ:url:%s, new_name:%s' %(imgUrl, fileName)) flag = self.uploadImgFile(fileName) #如果图片上传失败,返回None if flag == 1: logger.debug('upload img fail:url:%s, new_name:%s' %(imgUrl, fileName)) return None else: logger.debug('upload img succ:url:%s, new_name:%s' %(imgUrl, fileName)) self.client.hset(self.img_url_rec, imgUrl, fileName) return fileName except: logger.warn('getImage or uploadImage exception, url: %s'%(imgUrl)) return None else: return None def getHost(self, url): proto, rest = urllib.splittype(url) host, rest = urllib.splithost(rest) return '%s://%s' %(proto,host) def run(self, pack): task_id = pack['task_id'] cls = str(pack['class']) """ 如果该类没有配置则不需要充图 """ if cls not in self.pic_fill_conf: return 0 attr_conf = self.pic_fill_conf[cls] data_json = pack['data_json'] subject = data_json['subject'] must_fill_pic = 0 #考研的数据必须充图成功,否则丢弃 if cls == '46' and subject == u'考研': must_fill_pic = 1 if subject == u'动漫' or subject == u'摄影' or subject == u'程序员': return 0 for key in data_json: if attr_conf.has_key(key): field = data_json[key] way = attr_conf[key] try: #1.单值富文本充图 if way == '1': tree = etree.HTML(field) nodes=tree.xpath("//img/@src") for node in nodes: if node.startswith('http') or node.startswith('www'): target_url = node if node.startswith('www'): target_url = '%s%s'%('http://', node) else: #处理相对路径的url target_url = node.strip('../') url = data_json['url'] host = self.getHost(url) target_url = '%s/%s'%(host, target_url) new_img_name = self.getAndSaveImg(target_url) if new_img_name == None: if must_fill_pic == 1: return -1 continue else: new_data = self.pic_prefix + new_img_name reg_str = "\"[\S]*%s\"" %(node) reg = re.compile(reg_str) # new_str = '\"%s\"' %(new_data) field = re.sub(reg, '\"%s\"' %(new_data), field) data_json[key] = field #2.多值url充图 elif way == '2': new_pic_arr = [] for e in field: if e.startswith('http') or e.startswith('www'): target_url = e if e.startswith('www'): target_url = '%s%s'%('http://', e) else: #处理相对路径的url target_url = e.strip('../') url = pack['url'] host = self.getHost(url) target_url = '%s/%s'%(host, target_url) new_img_name = self.getAndSaveImg(target_url) if new_img_name == None: new_pic_arr.append(e) else: new_pic_arr.append(self.pic_prefix + new_img_name) data_json[key] = new_pic_arr #3.单值url充图 elif way == '3': e = field if e.startswith('http') or e.startswith('www'): target_url = e if e.startswith('www'): target_url = '%s%s'%('http://', e) else: #处理相对路径的url target_url = e.strip('../') url = pack['url'] host = self.getHost(url) target_url = '%s/%s'%(host, target_url) new_img_name = self.getAndSaveImg(target_url) data_json[key] = self.pic_prefix + new_img_name else: continue except: logger.warn('task_id:%s, attr:%s, way:%s picFill exception'%(task_id, key, way)) #将充完图的数据回写到pack包 pack['data_json'] = data_json logger.debug('taskid:%s, pic_fill succ.'%(task_id)) return 0
def initial_bucket(OSS_ID, OSS_KEY, OSS_HOST, OSS_DBNAME): """ 初始化oss bucket """ #print(OSS_ID) return oss2.Bucket(oss2.Auth(OSS_ID, OSS_KEY), OSS_HOST, OSS_DBNAME)
def __init__(self, accesskeyid, accesskeysecret): self.accesskeyid = accesskeyid self.accesskeysecret = accesskeysecret # 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 self.auth = oss2.Auth(self.accesskeyid, self.accesskeysecret)
def auth(self): return oss2.Auth(self.access_key_id, self.access_key_secret)
def to_excel(self, request, queryset): from xlwt import * path = MEDIA_ROOT + 'download_xls/' + request.user.username mkdir_p(MEDIA_ROOT + 'download_xls') os.popen('chmod 777 %s' % (MEDIA_ROOT + 'download_xls')) mkdir_p(path) os.popen('chmod 777 %s' % (path)) w = Workbook() sheet1 = w.add_sheet('price_check_1') sheet1.write(0, 0, u'SKU') sheet1.write(0, 1, u'商品编码') sheet1.write(0, 2, u'商品名称') sheet1.write(0, 3, u'原价') sheet1.write(0, 4, u'现价') sheet1.write(0, 5, u'申请人') sheet1.write(0, 6, u'申请时间') sheet2 = w.add_sheet('price_check_2') sheet2.write(0, 0, u'SKU') sheet2.write(0, 1, u'新供应商名称') sheet2.write(0, 2, u'采购人') sheet2.write(0, 3, u'责任归属人2') sheet3 = w.add_sheet('price_check_3') sheet3.write(0, 0, u'SKU') sheet3.write(0, 1, u'商品编码') sheet3.write(0, 2, u'新采购链接') sheet4 = w.add_sheet('price_check_4') sheet4.write(0, 0, u'SKU') sheet4.write(0, 1, u'商品编码') sheet4.write(0, 2, u'商品名称') sheet4.write(0, 3, u'原克重') sheet4.write(0, 4, u'现克重') # 写数据 row1 = 0 row2 = 0 row3 = 0 row4 = 0 for qs in queryset: b_goods_obj = py_b_goods.objects.filter(SKU=qs.GoodsSKU) if b_goods_obj.exists(): goodsname = b_goods_obj[0].GoodsName else: goodsname = None obj = sku_b_supplier.objects.filter(SupplierName=qs.NewSupplier) if obj.exists(): purchaser = obj[0].SupPurchaser possessMan2 = obj[0].Recorder else: purchaser = '' possessMan2 = '' row1 = row1 + 1 column1 = 0 sheet1.write(row1, column1, qs.GoodsSKU) column1 = column1 + 1 sheet1.write(row1, column1, qs.GoodsCode) column1 = column1 + 1 sheet1.write(row1, column1, goodsname) column1 = column1 + 1 sheet1.write(row1, column1, qs.OldPrice) column1 = column1 + 1 sheet1.write(row1, column1, qs.NowPrice) column1 = column1 + 1 sheet1.write(row1, column1, qs.SQStaffName) column1 = column1 + 1 sheet1.write(row1, column1, u'%s'%qs.SQTime) if qs.NewSupplier != '': row2 = row2 + 1 column2 = 0 sheet2.write(row2, column2, qs.GoodsSKU) column2 = column2 + 1 sheet2.write(row2, column2, qs.NewSupplier) column2 = column2 + 1 sheet2.write(row2, column2, purchaser) # 责任归属人2 column2 = column2 + 1 sheet2.write(row2, column2, possessMan2) if qs.NewSupplier != '' and qs.NewSupplierURL != None: row3 = row3 + 1 column3 = 0 sheet3.write(row3, column3, qs.GoodsSKU) column3 = column3 + 1 sheet3.write(row3, column3, qs.GoodsCode) column3 = column3 + 1 sheet3.write(row3, column3, qs.NewSupplierURL) if qs.NowWeight != '': row4 = row4 + 1 column4 = 0 sheet4.write(row4, column4, qs.GoodsSKU) column4 = column4 + 1 sheet4.write(row4, column4, qs.GoodsCode) column4 = column4 + 1 sheet4.write(row4, column4, goodsname) column4 = column4 + 1 sheet4.write(row4, column4, qs.OldWeight) column4 = column4 + 1 sheet4.write(row4, column4, qs.NowWeight) filename = request.user.username + '_' + datetime.now().strftime('%Y%m%d%H%M%S') + '.xls' w.save(path + '/' + filename) os.popen(r'chmod 777 %s' % (path + '/' + filename)) # 上传oss对象 auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET) bucket = oss2.Bucket(auth, ENDPOINT, BUCKETNAME_XLS) bucket.create_bucket(oss2.BUCKET_ACL_PUBLIC_READ) # 删除现有的 for object_info in oss2.ObjectIterator(bucket, prefix='%s/%s_' % (request.user.username, request.user.username)): bucket.delete_object(object_info.key) bucket.put_object(u'%s/%s' % (request.user.username, filename), open(path + '/' + filename)) messages.error(request, u'%s%s.%s/%s/%s' % (PREFIX, BUCKETNAME_XLS, ENDPOINT_OUT, request.user.username, filename) + u':成功导出,可点击Download下载到本地............................。')