OSS restful API 调用 put,上传文件,python发http request示例

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,恶意文件检测 1000次 1年
对象存储OSS,敏感数据保护2.0 200GB 1年
简介: 发送put 请求,向bucket中写入文件,代码中*** 的部分改成实际内容。rest请求主要问题在拼header时authorization可能会有问题,注意生成signature时的入参。#tested env: python version v3.9.6#author: Fred#2022-1-11import hmacimport hashlibimport base64im

发送put 请求,向bucket中写入文件,代码中*** 的部分改成实际内容。

rest请求主要问题在拼header时authorization可能会有问题,注意生成signature时的入参。

#tested env: python version v3.9.6
#author: Fred
#2022-1-11

import hmac
import hashlib
import base64
import datetime
import requests
from scrapy.utils.python import to_bytes

#this function is to get the md5 vaule for a file content,
#input argu: file path of the file you want to upload as the content of http request
#return: string of md5 vaule
#refer to https://www.alibabacloud.com/help/doc-detail/31951.html#section-i74-k35-5w4
def get_md5(file_name): 
    file = open(file_name, 'rb')
    hash = hashlib.md5()
    hash.update(file.read())
    return base64.b64encode(hash.digest()).decode('utf-8')

#this function is to calculate the signature, which is part of authentication, the info should be same with your http header
# argu refer to the link as below,
#refer to https://www.alibabacloud.com/help/en/doc-detail/31951.html
def get_sig(verb, content_md5, content_type, date, add_info_str, res):
    sig_param = verb + '\n' + content_md5 + '\n' + content_type + '\n' + date + '\n' + add_info_str + '\n' + res
    h = hmac.new(to_bytes(ak_secret), to_bytes(sig_param) , hashlib.sha1)
    return base64.b64encode(h.digest()).decode('utf-8')

#Here is the information provide by Cloud account owner, to access the Cloud resources
#ak_id means AccessKey ID
ak_id = '****'
#aksecret means AccessKey Secret
ak_secret = '****'
#host_addr means the url of the bucket
host_addr = '****.***.aliyuncs.com'
bucketname = '*****'
#dst_file_path should be the destination of your file, e.g. /SAP New/xxx.csv or /SAP Archived/xxx.csv
dst_file_path = '/***/samplefile.txt'

#Here is the infomation to fill the http request header
verb = 'PUT'
file_name = 'samplefile.txt'
content_md5 = get_md5(file_name)
content_type = 'text/plain'
#get GMT date
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
#CanonicalizedResource, destination bucketname and folder
res = '/****/***/samplefile.txt'
#CanonicalizedOSSHeaders, it's optional, if have multiple argu, need \n to seperate each
#https://www.alibabacloud.com/help/doc-detail/31951.html#section-rvv-dx2-xdb
author_info_key = 'x-oss-meta-author'
author_info_value = '***'
add_info_str = author_info_key + ':' + author_info_value

#Here to get the authentication so that our request will be permitted, the authentication info is necessary for http header
auth = "OSS " + ak_id + ":" + get_sig(verb, content_md5, content_type, date, add_info_str, res)

#Here to define the header include the authentication and other infomation
req_header = {
    'Host':host_addr,
    'Content-Md5':content_md5,
    'Content-Type':content_type,
    'Date':date,
    'Authorization':auth,
    author_info_key:author_info_value
}

##file_url = https://****.***.aliyuncs.com/***/samplefile.txt, url should include the folder name.
file_url ='https://' + host_addr + dst_file_path 
#send the request, file_url is the target full location of the file, headers is the http headers, data the the file content
req = requests.put(file_url, headers = req_header, data = open(file_name, 'rb').read())    

#show response
print(req)

相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
29天前
|
XML JSON API
淘宝商品详情API的调用流程(python请求示例以及json数据示例返回参考)
JSON数据示例:需要提供一个结构化的示例,展示商品详情可能包含的字段,如商品标题、价格、库存、描述、图片链接、卖家信息等。考虑到稳定性,示例应基于淘宝开放平台的标准响应格式。
|
1月前
|
API 开发者
了解 HTTP 的PUT 与 POST方法的综合指南
HTTP PUT 和 POST 方法是构建 Web 应用与 API 的核心工具,用于资源的创建与更新。PUT 方法通过指定 URL 更新或创建完整资源,具有幂等性;而 POST 方法更灵活,主要用于创建新资源,但不具备幂等性。本文详细对比了两者在请求体、URL 使用、资源处理等方面的区别,并提供了实际应用示例,帮助开发者根据场景选择合适的方法以优化 API 设计。
|
1月前
|
存储 JSON API
淘宝商品详情API接口概述与JSON数据示例
淘宝商品详情API是淘宝开放平台提供的核心接口之一,为开发者提供了获取商品深度信息的能力。以下是技术细节和示例:
|
2月前
|
JSON 搜索推荐 API
淘宝拍立淘按图搜索商品API接口示例说明
淘宝拍立淘按图搜索商品API接口是淘宝开放平台提供的一项基于图像识别技术的搜索服务,允许用户通过上传图片来快速找到相似的商品。以下是对该API接口的示例说明:
|
2月前
|
XML 数据挖掘 API
1688商品详情数据示例参考,1688API接口系列
在成长的路上,我们都是同行者。这篇关于详情API接口的文章,希望能帮助到您。期待与您继续分享更多API接口的知识,请记得关注Anzexi58哦!
|
1月前
|
存储 前端开发 安全
如何在自己的网站接入API接口获取数据?分步指南与实战示例
将第三方API(如微店API)接入网站是扩展功能和获取实时数据的关键。流程包括注册开发者账号、申请API权限、设置认证机制(OAuth 2.0或AppKey签名)、调用API实现前后端协作、处理数据与错误、优化安全性能,并解决常见问题。确保遵循最佳实践,保障系统稳定与安全。通过这些步骤,开发者可高效整合数据,提升应用功能。
|
3月前
|
JSON 前端开发 搜索推荐
关于商品详情 API 接口 JSON 格式返回数据解析的示例
本文介绍商品详情API接口返回的JSON数据解析。最外层为`product`对象,包含商品基本信息(如id、name、price)、分类信息(category)、图片(images)、属性(attributes)、用户评价(reviews)、库存(stock)和卖家信息(seller)。每个字段详细描述了商品的不同方面,帮助开发者准确提取和展示数据。具体结构和字段含义需结合实际业务需求和API文档理解。
|
3月前
|
JSON 缓存 API
解析电商商品详情API接口系列,json数据示例参考
电商商品详情API接口是电商平台的重要组成部分,提供了商品的详细信息,支持用户进行商品浏览和购买决策。通过合理的API设计和优化,可以提升系统性能和用户体验。希望本文的解析和示例能够为开发者提供参考,帮助构建高效、可靠的电商系统。
99 12
|
2月前
|
JSON API 数据格式
淘宝商品评论数据API接口详解及JSON示例返回
淘宝商品评论数据API接口是淘宝开放平台提供的一项服务,旨在帮助开发者通过编程方式获取淘宝商品的评论数据。这些数据包括评论内容、评论时间、评论者信息、评分等,对于电商分析、用户行为研究、竞品分析等领域都具有极高的价值。
|
2月前
|
JSON 安全 API
亚马逊商品详情API接口的使用方法和示例返回
亚马逊商品详情API接口是亚马逊为开发者提供的一个重要工具,它允许开发者通过编程方式获取亚马逊平台上的商品详细信息。以下是该接口的使用方法和示例返回的简要说明
下一篇
oss创建bucket