東川印記

一本東川,笑看爭龍斗虎;寰茫兦者,度橫佰昧人生。

配置 使用 aliyun oss python sdk 自动上传

2020年8月3日星期一



项目建立日期为

2020年01月13日16:57:50

当时没搞完,现在忘得差不多了。。。。


重新搞。。。。


1,安装 oss sdk

SENRSL:piaos-group senrsl$ python -V
Python 2.7.16
SENRSL:piaos-group senrsl$ git clone git@github.com:senRsl/aliyun-oss-python-sdk.git

SENRSL:piaos-group senrsl$ cd
.DS_Store              aliyun-oss-python-sdk/ publish/              
SENRSL:piaos-group senrsl$ cd aliyun-oss-python-sdk/

SENRSL:aliyun-oss-python-sdk senrsl$ sudo python setup.py install
Password:
running install
running bdist_egg
running egg_info
creating oss2.egg-info
writing requirements to oss2.egg-info/requires.txt

Using /Library/Python/2.7/site-packages
Finished processing dependencies for oss2==2.12.1
SENRSL:aliyun-oss-python-sdk senrsl$

验证安装

SENRSL:aliyun-oss-python-sdk senrsl$ cd ..
SENRSL:piaos-group senrsl$ cd publish/
SENRSL:publish senrsl$ python

WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.

Python 2.7.16 (default, Apr 17 2020, 18:29:03)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import oss2
>>> oss2.__version__
'2.9.1'
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> exit()
SENRSL:publish senrsl$

2,现成脚本

原来之前就写完了,重新拆分下。。。。

1)上传

SENRSL:publish senrsl$ cat upload.py
# -*- coding: utf-8 -*-
import oss2

from auth import *

# <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
# <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
bucket.put_object_from_file('2020/README.md', 'README.md')
SENRSL:publish senrsl$
           


2)列表

SENRSL:publish senrsl$ cat list.py
# -*- coding: utf-8 -*-
import oss2
import os
import sys

from auth import *

from itertools import islice

ls = os.system('ls')
print(ls)

pwd = os.system('pwd')
print(pwd)    # /Users/senrsl/android/Project/mtm/piaos-group/publish

# SENRSL:publish senrsl$ python list.py param1
arg1 = sys.argv[1]
print(arg1)    #param1

# oss2.ObjectIteratorr用于遍历文件。
for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)
SENRSL:publish senrsl$


3)删除

SENRSL:publish senrsl$ cat delete.py
# -*- coding: utf-8 -*-
import oss2


from auth import *


# <yourObjectName>表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
bucket.delete_object('2020/README.md')

SENRSL:publish senrsl$


4)以前的

SENRSL:publish senrsl$ cat composition.py
# -*- coding: utf-8 -*-
import oss2

from key import *


# 确认上面的参数都填写正确了
for param in (access_key_id, access_key_secret, bucket_name, endpoint):
    assert '<' not in param, '请设置参数:' + param

print(" ak is : " + access_key_id + "\n as is :" + access_key_secret)
print(" bucket is : " + bucket_name + "\n endpoint is : " + endpoint)

# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)

# 第一个参数 是云上路径
# 第二个参数 是本地路径
result = bucket.put_object_from_file('2020/README.md', 'README.md')

#print("result is : " + result)

# 列举Bucket下10个Object,并打印它们的最后修改时间、文件名
for i, object_info in enumerate(oss2.ObjectIterator(bucket)):
    print("{0} {1}".format(object_info.last_modified, object_info.key))

    if i >= 9:
        break
SENRSL:publish senrsl$


5)鉴权

SENRSL:publish senrsl$ cat auth.py
# -*- coding: utf-8 -*-
import os
import oss2

access_key_id = os.getenv('OSS_ACCESS_KEY_ID_PIAOS', 'LTAI4FusP')
access_key_secret = os.getenv('OSS_ACCESS_KEY_SECRET_PIAOS', 'MugZFiilbNEJSrrmO')
bucket_name = os.getenv('OSS_BUCKET_PIAOS', 'piaos-upgrade')
endpoint = os.getenv('OSS_ENDPOINT_PIAOS', 'https://oss-cn-beijing.aliyuncs.com')

# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth(access_key_id, access_key_secret)
# Endpoint以杭州为例,其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, endpoint, bucket_name)

SENRSL:publish senrsl$


3,微调

SENRSL:publish senrsl$ cat upload2.py
# -*- coding: utf-8 -*-
import os
import sys
import oss2

from auth import *

dirName = sys.argv[1]
if len(sys.argv)>2 : filePath = sys.argv[2]
else:
    print("bye from len");
    sys.exit();

# if dirName is None: dirName="20"

# if filePath is None :
#     print("bye");
#     sys.exit();

fileName = os.path.basename(filePath)
ossPath = dirName+'/'+fileName
print(filePath +"\t"+ fileName)

# <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
# <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
bucket.put_object_from_file(ossPath, filePath)

print("https://piaos-upgrade.oss-cn-beijing.aliyuncs.com/"+ossPath)

SENRSL:publish senrsl$

执行

SENRSL:publish senrsl$ python upload2.py debug /Users/senrsl/Downloads/Post18.apk
/Users/senrsl/Downloads/Post18.apk    Post18.apk
https://piaos-upgrade.oss-cn-beijing.aliyuncs.com/debug/Post18.apk
SENRSL:publish senrsl$


又填完了一个坑,挖了一堆新坑!

2020年08月03日15:47:49


--
senRsl
2020年08月03日11:52:33

没有评论 :

发表评论