OSS restful API 调用 get,遍历目录中的文件,python发http request示例

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储 OSS,内容安全 1000次 1年
对象存储 OSS,恶意文件检测 1000次 1年
简介: 发送get 请求,遍历目录下的所有文件,代码中*** 的部分改成实际内容,这个API说明文档在bucket操作里面。rest请求主要问题在拼header时authorization可能会有问题,注意计算签名时的入参。#tested env: python version v3.9.6#author: Fred#2022-1-11import hmacimport hashlibimpo

发送get 请求,遍历目录下的所有文件,代码中*** 的部分改成实际内容,这个API说明文档在bucket操作里面。

rest请求主要问题在拼header时authorization可能会有问题,注意计算签名时的入参。

#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(content): 
    hash = hashlib.md5()
    hash.update(to_bytes(content))
    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'

#Here is the infomation to fill the http request header
verb = 'GET'
content_md5 = get_md5('')
content_type = 'text/html'
#get GMT date
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
#CanonicalizedResource, destination bucketname and folder
res = '/****/'
#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,
    
    #the params below is for query the files in folder
    #https://www.alibabacloud.com/help/en/doc-detail/187544.html
    
    #List-type: this is the version of API, must be 2    
    'List-type': '2',
    # when set Delimiter to /, it will only return sun-folder name but not list the files in the sub-folder, else will also display the files in sub-folder
    'Delimiter': '/',
    #prefix means the folder name, please take notice of /
    'Prefix': '***/'
}


##request url is the bucket addr
req_url ='https://' + host_addr
req = requests.get(req_url, headers = req_header)    

#show response
#status should be 200
print(req.status_code)
print(req.headers)

#the content is in xml format,
#refer to: https://www.alibabacloud.com/help/en/doc-detail/187544.html
print(req.content)

相关实践学习
借助OSS搭建在线教育视频课程分享网站
本教程介绍如何基于云服务器ECS和对象存储OSS,搭建一个在线教育视频课程分享网站。
目录
相关文章
|
3天前
|
JSON API 数据格式
Python网络编程:HTTP请求(requests模块)
在现代编程中,HTTP请求几乎无处不在。无论是数据抓取、API调用还是与远程服务器进行交互,HTTP请求都是不可或缺的一部分。在Python中,requests模块被广泛认为是发送HTTP请求的最简便和强大的工具之一。本文将详细介绍requests模块的功能,并通过一个综合示例展示其应用。
|
1天前
|
缓存 Java API
从零到一:构建一个高效的 RESTful API 服务
本文将详细介绍如何从头开始设计和实现一个高效的 RESTful API 服务。我们将探讨 API 设计的最佳实践、选择合适的技术栈、实现常见功能(如认证、数据验证、错误处理)以及优化性能的策略。通过实例代码和实际应用场景的分析,读者将能够掌握构建高效且易于维护的 API 服务的关键步骤和技巧。
|
7天前
|
Java API 数据库
【神操作!】Spring Boot打造RESTful API:从零到英雄,只需这几步,让你的Web应用瞬间飞起来!
【8月更文挑战第12天】构建RESTful API是现代Web开发的关键技术之一。Spring Boot因其实现简便且功能强大而深受开发者喜爱。本文以在线图书管理系统为例,展示了如何利用Spring Boot快速构建RESTful API。从项目初始化、实体定义到业务逻辑处理和服务接口实现,一步步引导读者完成API的搭建。通过集成JPA进行数据库操作,以及使用控制器类暴露HTTP端点,最终实现了书籍信息的增删查改功能。此过程不仅高效直观,而且易于维护和扩展。
19 1
|
8天前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
31 1
|
8天前
|
开发者 Python
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
21 1
|
8天前
|
缓存 测试技术 API
从零到一:构建高效的 RESTful API 服务器
在当今的软件开发环境中,RESTful API 是实现系统间数据交互的关键组件。本文探讨了如何从头开始构建一个高效的 RESTful API 服务器,包括技术选型、架构设计、性能优化等方面的内容。我们将以 Python 的 Flask 框架为例,展示如何设计一个可扩展且高性能的 API 服务器,并提供实际代码示例来说明最佳实践。
|
10天前
|
API 开发者
深入浅出:RESTful API设计的艺术与实践
在数字化浪潮的推动下,后端开发如同编织一张张网络,连接着信息世界的每个角落。本文将带你领略RESTful API设计的精妙之处,从理论到实践,一步步揭示如何打造高效、易于维护和扩展的服务接口。我们将探索API设计的基本原则、最佳实践,以及如何避免常见的陷阱,让你的后端服务如同优雅的舞者,在数据的海洋中轻盈起舞。
|
10天前
|
存储 API 网络架构
深入浅出:后端开发中的RESTful API设计原则
在数字化浪潮的推动下,后端开发成为构建现代网络服务不可或缺的一环。本文将引导你理解RESTful API的设计哲学,探索如何通过简洁而强大的设计来提升后端服务的可维护性与扩展性。我们将从REST的基本概念出发,逐步深入到API设计的实战技巧,最终实现高效且优雅的后端架构。
|
16天前
|
存储 缓存 API
深入理解RESTful API设计原则与实践
在现代Web开发中,RESTful API已成为一种广泛采用的架构风格。它基于Representational State Transfer (REST)原则,强调资源的状态转化。本文将探讨RESTful API的核心概念、设计原则以及如何在实际项目中应用这些理念来构建高效、可维护的后端服务。文章还将分享一些最佳实践和常见误区,帮助开发者更好地理解和运用RESTful API设计。