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

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储OSS,敏感数据保护2.0 200GB 1年
对象存储 OSS,恶意文件检测 1000次 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)

相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
目录
相关文章
|
存储 缓存 JavaScript
python实战篇:利用request库打造自己的翻译接口
python实战篇:利用request库打造自己的翻译接口
241 1
python实战篇:利用request库打造自己的翻译接口
|
网络安全 Python
python request SSL error 403证书错误
python request SSL error 403证书错误
194 0
|
存储
HTTP的PUT请求是干什么的?底层原理是什么?
HTTP的PUT请求是干什么的?底层原理是什么?
1850 3
|
5月前
|
数据采集 存储 开发者
如何动态调整Python爬虫的Request请求延迟
如何动态调整Python爬虫的Request请求延迟
202 13
|
12月前
|
JSON Go API
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
使用Go语言和Gin框架构建RESTful API:GET与POST请求示例
|
11月前
|
存储 前端开发 API
Restful API 设计示例
Restful API 设计示例
106 0
python并发执行request请求
选择哪种并发方式取决于我们的具体需求。对于I/O密集型任务,多线程或异步I/O通常是更好的选择;对于CPU密集型任务,多进程可能是更好的选择。此外,异步I/O通常比多线程具有更好的性能,特别是在高并发的网络应用中。
|
API Python
Flask-RESTful基础示例
Flask-RESTful基础示例
|
文字识别 小程序 算法
视觉智能开放平台产品使用合集之通过小程序接入视觉智能开放平台API能力,不是上海地域的OSS链接,该怎么办
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
153 0
|
机器学习/深度学习 人工智能 分布式计算
人工智能平台PAI产品使用合集之在easy_rec中,将model_dir设置为oss地址时,oss相关配置需要加载在环境中,有完整的示例吗
阿里云人工智能平台PAI是一个功能强大、易于使用的AI开发平台,旨在降低AI开发门槛,加速创新,助力企业和开发者高效构建、部署和管理人工智能应用。其中包含了一系列相互协同的产品与服务,共同构成一个完整的人工智能开发与应用生态系统。以下是对PAI产品使用合集的概述,涵盖数据处理、模型开发、训练加速、模型部署及管理等多个环节。

热门文章

最新文章

推荐镜像

更多