创建BUCKET脚本
安装模块
pip install pymysql
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-rds
pip install aliyun-python-sdk-ram
脚本如下
#!/usr/bin/env python
#coding=utf-8
# author:LJX
# createdate:2020-05-09
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkram.request.v20150501.CreateUserRequest import CreateUserRequest
from aliyunsdkram.request.v20150501.CreateAccessKeyRequest import CreateAccessKeyRequest
from aliyunsdkram.request.v20150501.CreatePolicyRequest import CreatePolicyRequest
from aliyunsdkram.request.v20150501.AttachPolicyToUserRequest import AttachPolicyToUserRequest
import pymysql
import oss2
import datetime
class bucket:
def __init__(self,accessKeyId,accessSecret):
self.accessKeyId = accessKeyId
self.accessSecret = accessSecret
self.client = AcsClient(self.accessKeyId, self.accessSecret, 'cn-shenzhen')
self.auth = oss2.Auth(self.accessKeyId, self.accessSecret)
self.date = datetime.datetime.now().strftime('%Y%m%d')
self.conn = pymysql.connect(host='',user='',password='',database='',charset="utf8")
# 创建RAM用户
def createRam(self,UserName):
request = CreateUserRequest()
request.set_accept_format('json')
request.set_UserName(UserName)
self.exec(request)
# 创建accessKey
def createAccess(self,UserName):
request = CreateAccessKeyRequest()
request.set_accept_format('json')
request.set_UserName(UserName)
data = self.exec(request)
return data
# 创建授权策略
def CreatePolicy(self,bucket,PolicyName):
request = CreatePolicyRequest()
request.set_accept_format('json')
request.set_PolicyName(PolicyName)
Policy_key = "{"Version":"1","Statement":[{"Effect":"Allow","Action":"oss:*","Resource":["acs:oss:*:*:"+bucket+"","acs:oss:*:*:"+bucket+"/*"]},{"Effect":"Allow","Action":"oss:ListBuckets","Resource":"acs:oss:*:*:*"},{"Effect":"Allow","Action":["oss:GetObject","oss:GetObjectAcl"],"Resource":"acs:oss:*:*:"+bucket+"/*"}]}"
request.set_PolicyDocument(str(Policy_key))
self.exec(request)
# 授权
def AttachPolicyToUser(self,PolicyName,UserName):
request = AttachPolicyToUserRequest()
request.set_accept_format('json')
request.set_PolicyType("Custom")
request.set_PolicyName(PolicyName)
request.set_UserName(UserName)
self.exec(request)
# 创建bucket
def createbucket(self,yourBucketName):
bucket = oss2.Bucket(self.auth, 'http://cn-shenzhen.oss.aliyuncs.com', yourBucketName)
bucket.create_bucket(oss2.models.BUCKET_ACL_PUBLIC_READ)
def exec(self,request):
response = self.client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))
def workon(self):
yourBucketName = input('请输入bucket名称')
PolicyName = yourBucketName + '-oss'
UserName = yourBucketName+'_user'
# 第一步、创建RAM用户
try:
self.createRam(UserName)
except Exception as e:
print(e)
try:
data = self.createAccess(UserName)
cursor = self.conn.cursor()
sql = """INSERT INTO `yunwei_info`.`bucketinfo`(
`AccessKeyId`,
`AccessKeySecret`,
`UserName`,
`PolicyName`,
`Bucket`
) VALUES(
{0},{1},{2},{3},{4})""".format(data['AccessKey']['AccessKeyId'],data['AccessKey']['AccessKeySecret'],UserName,PolicyName,yourBucketName)
cursor.execute(sql)
cursor.close()
self.conn.close()
except Exception as e:
print(e)
# 第二步、创建bucket
try:
self.createbucket(yourBucketName)
except Exception as e:
print(e)
# 第三步、创建授权规则
try:
self.CreatePolicy(yourBucketName,PolicyName)
except Exception as e:
print(e)
# 第四步、授权bucket
try:
self.AttachPolicyToUser(PolicyName,UserName)
except Exception as e:
print(e)
if __name__ == "__main__":
accessKeyId = 'xxxxx'
accessSecret = 'xxxxxx'
b = bucket(accessKeyId,accessSecret)
b.workon()