网站首页 > 技术文章 正文
Amazon Web Services (AWS) 提供了 Simple Storage Service (S3) 作为云存储解决方案。通过 S3,用户可以存储和检索任意数量的数据,从任何地方访问。要使用 Python 与 AWS S3 进行交互,通常会使用 AWS SDK for Python,也称为 Boto3。
安装 Boto3
首先,确保安装了 Boto3。如果还没有安装,可以通过 pip 安装:
Bash
深色版本
1pip install boto3
配置 AWS 凭证
在开始之前,需要配置 AWS 凭证。这可以通过多种方式完成,包括但不限于:
- 环境变量:
- Bash
- 深色版本
- 1export AWS_ACCESS_KEY_ID=<your_access_key> 2export AWS_SECRET_ACCESS_KEY=<your_secret_key>
- ~/.aws/credentials 文件:
- Ini
- 深色版本
- 1[default] 2aws_access_key_id = <your_access_key> 3aws_secret_access_key = <your_secret_key>
- 直接在代码中设置 (不建议,仅适用于测试目的):
- Python
- 深色版本
- 1import boto3 2 3s3 = boto3.client( 4 's3', 5 aws_access_key_id='<your_access_key>', 6 aws_secret_access_key='<your_secret_key>' 7)
基本示例
接下来是一些使用 Boto3 与 S3 进行交互的基本示例。
创建 S3 客户端
Python
深色版本
1import boto3
2
3# 创建 S3 客户端
4s3_client = boto3.client('s3')
列出所有 S3 存储桶
Python
深色版本
1# 列出所有存储桶
2response = s3_client.list_buckets()
3
4# 打印存储桶名称
5for bucket in response['Buckets']:
6 print(bucket['Name'])
创建新的 S3 存储桶
Python
深色版本
1bucket_name = 'my-new-bucket'
2
3# 创建一个新的存储桶
4s3_client.create_bucket(Bucket=bucket_name)
上传文件到 S3
Python
深色版本
1file_path = '/path/to/local/file.txt'
2key = 'file.txt' # S3 中的对象键名
3
4# 上传文件
5s3_client.upload_file(file_path, bucket_name, key)
下载文件从 S3
Python
深色版本
1destination_path = '/path/to/download/file.txt'
2
3# 下载文件
4s3_client.download_file(bucket_name, key, destination_path)
删除 S3 对象
Python
深色版本
1# 删除特定对象
2s3_client.delete_object(Bucket=bucket_name, Key=key)
3
4# 删除整个存储桶及其内容
5response = s3_client.list_objects_v2(Bucket=bucket_name)
6for obj in response.get('Contents', []):
7 s3_client.delete_object(Bucket=bucket_name, Key=obj['Key'])
8
9# 删除空存储桶
10s3_client.delete_bucket(Bucket=bucket_name)
示例代码
下面是将上述功能组合在一起的一个完整示例:
Python
深色版本
1import boto3
2
3def list_buckets(s3_client):
4 """列出所有的 S3 存储桶"""
5 response = s3_client.list_buckets()
6 return [bucket['Name'] for bucket in response['Buckets']]
7
8def create_bucket(s3_client, bucket_name):
9 """创建一个新的 S3 存储桶"""
10 s3_client.create_bucket(Bucket=bucket_name)
11
12def upload_file(s3_client, file_path, bucket_name, key):
13 """上传文件到 S3"""
14 s3_client.upload_file(file_path, bucket_name, key)
15
16def download_file(s3_client, bucket_name, key, destination_path):
17 """从 S3 下载文件"""
18 s3_client.download_file(bucket_name, key, destination_path)
19
20def delete_object(s3_client, bucket_name, key):
21 """删除 S3 中的对象"""
22 s3_client.delete_object(Bucket=bucket_name, Key=key)
23
24def delete_bucket(s3_client, bucket_name):
25 """删除 S3 存储桶及其内容"""
26 response = s3_client.list_objects_v2(Bucket=bucket_name)
27 for obj in response.get('Contents', []):
28 s3_client.delete_object(Bucket=bucket_name, Key=obj['Key'])
29 s3_client.delete_bucket(Bucket=bucket_name)
30
31if __name__ == '__main__':
32 s3_client = boto3.client('s3')
33
34 # 创建一个新的存储桶
35 bucket_name = 'example-bucket'
36 create_bucket(s3_client, bucket_name)
37
38 # 上传文件
39 file_path = '/path/to/local/file.txt'
40 key = 'file.txt'
41 upload_file(s3_client, file_path, bucket_name, key)
42
43 # 列出存储桶
44 buckets = list_buckets(s3_client)
45 print("Current buckets:", buckets)
46
47 # 下载文件
48 destination_path = '/path/to/download/file.txt'
49 download_file(s3_client, bucket_name, key, destination_path)
50
51 # 删除文件
52 delete_object(s3_client, bucket_name, key)
53
54 # 删除存储桶
55 delete_bucket(s3_client, bucket_name)
这个脚本包含了与 S3 进行基本交互所需的所有功能。你可以根据自己的需求修改和扩展这些功能。


猜你喜欢
- 2024-12-23 如何在你的项目中混合 Rust 和 Python
- 2024-12-23 一秒开挂!纯 Python 开发 Web 应用
- 2024-12-23 用 Python 与 Windows 交互 - Pywin32库
- 2024-12-23 交互式环境(Python Shell)编写Python代码
- 2024-12-23 这个用Python编写的大数据测试工具,我给100分
- 2024-12-23 Python与Mysql交互库(持续更新)
- 2024-12-23 Python解释器和交互模式
- 2024-12-23 有用的 Python 提示和技巧 — #5
- 2024-12-23 Python 语言如何和 C/C++ 语言交互使用
- 2024-12-23 简单学Python——做一个可交互的图(结合ipywidgets库)
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)