2019独角兽企业重金招聘Python工程师标准>>>
列子公共读
:
新建一个bucket - > data
存储目录
-> 权限
-> 权限策略管理
# 只读方式
{
"Statement": [
{
"Action": [
"oss:Get*",
"oss:List*"
],
"Effect": "Allow",
"Resource": [
"acs:oss:*:*:data",
"acs:oss:*:*:data/*"
]
}
],
"Version": "1"
}
#读写方式
{
"Statement": [
{
"Action": "oss:*",
"Effect": "Allow",
"Resource": [
"acs:oss:*:*:data",
"acs:oss:*:*:data/*"
]
}
],
"Version": "1"
}
# 指定可读写路径 /2019/write
{
"Version": "1",
"Statement": [
{
"Action": "oss:*",
"Effect": "Allow",
"Resource": [
"acs:oss:*:*:/2019/write",
"acs:oss:*:*:/2019/write/*"
]
},
{
"Effect": "Allow",
"Action": [
"oss:ListObjects"
],
"Resource": [
"acs:oss:*:*:2019"
],
"Condition": {
"StringLike": {
"oss:Delimiter": "/",
"oss:Prefix": [
"",
"write/",
"write/*"
]
}
}
}
]
}
新建用户
回到oss bucket - > data
目录授权
上传下载阿里云oss文件
安装python模块
pip install -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com oss2
#pip install --upgrade --force-reinstall 'requests==2.6.0' urllib3
oss_put.py 上传
# -*- coding: utf-8 -*-
import oss2
# 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('LTAIPxxxx', 'jK5bjnxxxxxxxx')
# Endpoint以杭州为例,其它Region请按实际情况填写。
#bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'data')
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou-internal.aliyuncs.com', 'data') # VPC内网上传
bucket.put_object_from_file('test/20190125.log', '/root/out.log')
# 将系统out.log文件上传到远端test目录创建新文件名
下载oss文件到本地
bucket.get_object_to_file('test/123', 'down.txt')
# 替换put方法;下载远端test目录123到本地
api错误信息: https://error-center.aliyun.com/status/product/Oss
实例文件: https://help.aliyun.com/document_detail/32026.html?spm=a2c4g.11186623.6.699.1a8a5779dROfRu