1.获取Access Key和Secret Key

创建了obs资源后,进入 我的凭证->访问密钥->新增访问密钥
Ps:只能创建两次密钥,一定要保存好!!!

2.Python接口访问

连接S3

主机名和auth_region_name根据自己所在区域而定,我这里是cn-north-4

import boto
import boto.s3.connectionaccess_key = 'put your access key here!'
secret_key = 'put your secret key here!'conn = boto.connect_s3(aws_access_key_id = access_key,aws_secret_access_key = secret_key,host = ' obs.cn-north-4.myhuaweicloud.com',#is_secure=False,               # uncomment if you are not using sslcalling_format = boto.s3.connection.OrdinaryCallingFormat(),)
conn.auth_region_name = 'cn-north-4'

创建桶

conn.create_bucket(bucket_name,headers=None,location='cn-north-4', policy=None)

删除桶

必须为空桶才能删除

conn.delete_bucket(bucket.name)

列举所有桶的对象

for bucket in conn.get_all_buckets():print ("{name}\t{created}".format(name = bucket.name,created = bucket.creation_date,))

列举某个桶的对象

for key in bucket.list():print "{name}\t{size}\t{modified}".format(name = key.name,size = key.size,modified = key.last_modified,)

上传对象

bucket = self.conn.get_bucket(bucket_name)key = bucket.new_key(object_name)key.set_contents_from_filename(object_path)

删除对象

bucket.delete_key('goodbye.txt')

下载对象到本地

key = bucket.get_key('perl_poetry.pdf')
key.get_contents_to_filename('/home/larry/documents/perl_poetry.pdf')

下载对象(url)

hello_key = bucket.get_key('hello.txt')
hello_url = hello_key.generate_url(0, query_auth=False, force_http=True)
print hello_urlplans_key = bucket.get_key('secret_plans.txt')
plans_url = plans_key.generate_url(3600, query_auth=True, force_http=True)
print plans_url

封装成类

S3_api.py:

import boto.s3.connection
from functools import wrapsACCESS_KEY = 'put your access key here!'
SECRET_KEY = 'put your secret key here!'
HOST = 'obs.cn-north-4.myhuaweicloud.com'def is_exist_bucket(func):@wraps(func)def is_exist_bucket_decorator(self, *args, **kwargs):bucket_name = args[0]if bucket_name not in [bucket.name for bucket inself.conn.get_all_buckets()]:self.conn.create_bucket(bucket_name)# print(func(*args, **kwargs))return func(self,*args, **kwargs)return is_exist_bucket_decoratorclass S3:def __init__(self):self.url_validity = 3600self.conn = boto.connect_s3(aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY,host=HOST,is_secure=False,calling_format=boto.s3.connection.OrdinaryCallingFormat(),)self.conn.auth_region_name = 'cn-north-4'@is_exist_bucketdef upload_object(self, bucket_name, object_name, object_path):try:bucket = self.conn.get_bucket(bucket_name)key = bucket.new_key(object_name)key.set_contents_from_filename(object_path)return Trueexcept Exception as e:print("upload {} error:{}".format(object_name, e))return Falsedef get_object_url(self, bucket_name, object_name):try:bucket = self.conn.get_bucket(bucket_name)plans_key = bucket.get_key(object_name)plans_url = plans_key.generate_url(self.url_validity,query_auth=True,force_http=False)return plans_urlexcept Exception as e:print("get {} error:{}".format(object_name, e))return Falsedef download_object(self, bucket_name, object_names, data_path,compressed_name=None):try:bucket = self.conn.get_bucket(bucket_name)key = bucket.get_key(object_names)key.get_contents_to_filename(data_path)return Trueexcept Exception as e:print("download error: %s" % e)return Falsedef list_bucket_content(self, bucket_name):try:bucket = self.conn.get_bucket(bucket_name)for key in bucket.list():print("{name}\t{size}\t{modified}".format(name=key.name,size=key.size,modified=key.last_modified,))except Exception as e:print("get {} error:{}".format(bucket_name, e))return Falsedef create_bucket(self, bucket_name):try:self.conn.create_bucket(bucket_name,headers=None,location='cn-north-4', policy=None)return Trueexcept Exception as e:print("get {} error:{}".format(bucket_name, e))return Falsedef delete_bucket(self, bucket_name):try:self.conn.delete_bucket(bucket_name)except Exception as e:print("get {} error:{}".format(bucket_name, e))return Falsedef delete_object(self,bucket_name,object_name):try:bucket = self.conn.get_bucket(bucket_name)bucket.delete_key(object_name)return Trueexcept Exception as e:print("get {} error:{}".format(bucket_name, e))return False

测试

test.py:

from s3_api import S3
s=S3()
s.list_bucket_content('obs-f1bd')
print(s.get_object_url('obs-f1bd', 'kid.jpg'))
#s.download_object('obs-f1bd', 'kid.jpg', 'D:/kid3.jpg')
#s.create_bucket('xm-bucket')
#s.upload_object('xm-bucket','kid.jpg','D:/kid2.jpg')

S3接口访问华为云OBS相关推荐

  1. 使用rclone工具实现华为云OBS至AWS S3数据迁移同步

    1. 背景 项目需要将华为云的OBS对象存储服务的存储桶bucket的内容迁移复制到AWS云的S3存储桶中,AWS中暂无实现改需求的云服务,所以采用开源的第三方软件rclone来实现. rclone可 ...

  2. 华为云 obs 文件上传 及防盗链设置有效时间访问链接

    华为官方文档位置 本文适合小白和新手 ,仅满足基本文件上传 和 访问 maven引入华为云 obs jar文件 <dependency><groupId>com.huaweic ...

  3. ElasticSearch基于snapshot和华为云OBS的备份

    微信公众号:运维开发故事,作者:double冬 0 背景 任何一个存储数据的软件,都需要定期的备份数据.es replica提供了运行时的高可用保障机制,可以容忍少数节点的故障和部分数据的丢失,但是整 ...

  4. sscom串口网络数据调试器使用post方法向华为云obs桶上传文件和图片

    原贴地址:sscom串口网络数据调试器使用post方法向华为云obs桶上传文件和图片-云社区-华为云 [摘要] 之前发了文章"postman使用post方法向华为云obs桶上传文件和图片&q ...

  5. uniapp 上传图片到华为云obs

    记录一下用uniapp上传图片到华为云obs,之前是先把文件传到我们自己的服务器,然后后端的同事再上传到obs,但是我们公司的带宽太低了,传的太太太太慢了,于是考虑直接让用户上传到obs,不经过我们自 ...

  6. postman使用put方法向华为云obs桶上传文件和图片

    原贴地址:https://bbs.huaweicloud.com/blogs/298147 [摘要] postman使用put方法向华为云obs桶上传文件和图片.记录下过程. postman使用put ...

  7. 华为云OBS深度体验之迁移

    一.背景 对象存储我相信并不是什么新鲜的概念,最早的时候我们采用自建NFS的方式来实现文件共享,随着云计算的发展,逐渐就衍生除了对象存储服务,华为云有OBS,阿里云有OSS,腾讯云有COS,各大云计算 ...

  8. vue 上传文件到华为云obs

    有两种方式, 第一种是在前端直接上传文件到obs, 第二种是先把文件上传到后台, 然后后台再调用obs对应开发语言的SDK 1. 前端直接上传文件到obs,不经过后端 1.1 使用npm引入包 // ...

  9. 前端js华为云obs断点续传上传

    前端js华为云obs断点续传上传 断点续传上传就是将待上传的文件分成若干份分别上传,并实时地将每段上传结果统一记录在断点续传记录对象中,仅当所有分段都上传成功时返回上传成功的结果,否则在回调函数中返回 ...

  10. PHP后端生成签名后uniapp前端直传华为云OBS记录

    uniapp 官方合作的云存储是阿里云和腾讯云,但我们公司选的云存储服务恰好都不是这两家,是华为云 OBS.网上搜了多次,没有能成功的案例分享.但是,考虑到网上关于华为云性能优异的测评报告,以及我对华 ...

最新文章

  1. 移动端1px线的实现
  2. Redis 热点key
  3. 线上CPU100%?看看这篇是怎么排查的!
  4. 你真的理解a -- -- a a++ ++a 吗?
  5. 【XSY2470】lcm 数学
  6. FFmpeg拼接文件时报错channel element 1.0 is not allocated的分析思路和解决方法
  7. PUN 2 菜鸟养成记 2主服务
  8. javascript之Math
  9. 记录下学习的mac 自带apache 使用方法,及xamp的使用
  10. B2B2C多租户商城系统解决方案:打通线上线下服务体验,提升企业品牌渗透力
  11. 书到用时方恨少的Android
  12. 想成为3D建模师,只学习3dsmax就够了吗?
  13. 基于STM32红外计数的灯光照明
  14. 两个案例了解购物平台的发票制度
  15. Vue组件-调用摄像头识别二维码
  16. Failed to load config plugin:vue/essenti al to extend from.
  17. 中科磐云 隐写术应用
  18. 德国超市纷纷推出订购自取服务
  19. linux交换分区的文件格式为,linux利用交换分区空间类提供虚拟内存,交换分区的文件系统类型必须是() (5.0分)...
  20. MRO采购对酒店业务的重要性

热门文章

  1. 自主上传图片投票工具、上传图片投票小程序、上传图片投票平台
  2. Kubeadm部署单Master节点
  3. python tcl quartus_使用TCL脚本语言操作Quartus(一)
  4. 使用c++语言做概率论 涉及求方差
  5. log4j2 的使用【超详细图文】
  6. 模拟时钟c语言编码,C语言模拟时钟转动程序
  7. 布隆过滤器与布谷鸟过滤器(经典版)
  8. 关于Maven,这些应该够用了
  9. 按夏普计算机技巧,股票投资策略:怎样用夏普比率Sharpe Ratio寻找强势股
  10. 文字转语音文件现成工具