boto3--通过Python的SDK连接aws
通过Python的SDK连接aws
参考:
https://aws.amazon.com/cn/developers/getting-started/python/
aws上生成访问密钥 ID 和私有访问密钥
1.Windows连接方式:
1 . 通过Windows连接到 aws服务器时,需要通过“访问密钥(访问密钥 ID 和私有访问密钥)”
2 . 需要在C:\Users\admin\.aws中创建credentials文件(.aws目录不存在需要手动创建admin为你当前系统用户家目录)
cat /c/Users/admin/.aws/credentials
[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxx
3 . 安装boto3模块
pip install boto3
4 . 列出已经存在的s3存储桶
import boto3
s3client = boto3.client("s3")
for bucket in s3client.list_buckets()["Buckets"]:
print(bucket)
输出:
{'Name': 'xxxxxxs3', 'CreationDate': datetime.datetime(2017, 8, 2, 4, 16, 31, tzinfo=tzutc())}
{'Name': 'testxxxxxs3', 'CreationDate': datetime.datetime(2017, 8, 2, 6, 57, 29, tzinfo=tzutc())}
2.Linux连接方式:
1 . 安装boto3模块
pip install boto3
2 . 创建.aws目录
[ec2-user@LiBaolin-Singapore#>> ~]$mkdir .aws
3 . 创建credentials文件
[ec2-user@LiBaolin-Singapore#>> ~/.aws]$cat credentials
[default]
aws_access_key_id = xxxxxxxxxxxxxxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxx
4 . clone一个测试实例
[ec2-user@LiBaolin-Singapore#>> ~]$git clone https://github.com/awslabs/aws-python-sample.git
5 . 运行测试实例(会创建一个存储桶,并创建一个文件,最后删除该存储桶)
[ec2-user@LiBaolin-Singapore#>> ~/aws-python-sample]$python s3_sample.py
6 . s3_sample.py代码如下:
import boto3
import uuid
s3client = boto3.client('s3')
bucket_name = 'python-sdk-sample-{}'.format(uuid.uuid4())
print('Creating new bucket with name: {}'.format(bucket_name))
s3client.create_bucket(Bucket=bucket_name)
list_buckets_resp = s3client.list_buckets()
for bucket in list_buckets_resp['Buckets']:
if bucket['Name'] == bucket_name:
print('(Just created) --> {} - there since {}'.format(
bucket['Name'], bucket['CreationDate']))
object_key = 'python_sample_key.txt'
print('Uploading some data to {} with key: {}'.format(
bucket_name, object_key))
s3client.put_object(Bucket=bucket_name, Key=object_key, Body=b'Hello World!')
url = s3client.generate_presigned_url(
'get_object', {'Bucket': bucket_name, 'Key': object_key})
print('\nTry this URL in your browser to download the object:')
print(url)
input("\nPress enter to continue...")
print('\nNow using Resource API')
s3resource = boto3.resource('s3')
bucket = s3resource.Bucket(bucket_name)
obj = bucket.Object(object_key)
print('Bucket name: {}'.format(bucket.name))
print('Object key: {}'.format(obj.key))
print('Object content length: {}'.format(obj.content_length))
print('Object body: {}'.format(obj.get()['Body'].read()))
print('Object last modified: {}'.format(obj.last_modified))
print('\nDeleting all objects in bucket {}.'.format(bucket_name))
## 以下部分为 删除刚才创建的存储桶,为了看到效果可以先注释,而后在手动删除
delete_responses = bucket.objects.delete()
for delete_response in delete_responses:
for deleted in delete_response['Deleted']:
print('\t Deleted: {}'.format(deleted['Key']))
print('\nDeleting the bucket.')
bucket.delete()
boto3--通过Python的SDK连接aws的更多相关文章
- 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...
- Java通过aws java sdk在AWS S3上进行操作客户端
有两种方式可以安装aws java sdk:方式一:可以在这个网站上下载适用于 Java 的 AWS 开发工具包https://aws.amazon.com/cn/sdk-for-java/点击网页右 ...
- MinIO Python Client SDK 快速入门指南
官方文档地址:http://docs.minio.org.cn/docs/master/python-client-quickstart-guide MinIO Python Client SDK提供 ...
- python 使用pymssql连接sql server数据库
python 使用pymssql连接sql server数据库 #coding=utf-8 #!/usr/bin/env python#------------------------------ ...
- Python中HTTPS连接
permike 原文 Python中HTTPS连接 今天写代码时碰到一个问题,花了几个小时的时间google, 首先需要安装openssl,更新到最新版本后,在浏览器里看是否可访问,如果是可以的,所以 ...
- Ubuntu 14.04 配置vsftpd实现FTP服务器 - 通过FTP连接AWS
测试主机:亚马逊AWS EC2 系统:Ubuntu 14.04 想用AWS来做服务器玩,结果发现其不能像简单使用阿里云服务器那样用ftp连接,反正也不熟悉ftp服务器搭建,那就乘这个机会学习一下如何利 ...
- 亚马逊的PuTTY连接AWS出现network error connection refused,终极解决方案。
使用PuTTY连接AWS的时候,一直出现network error connection refused.百度了这个问题,大家都说是SSH要设置成22.但是我已经设置过了,为什么还是遇到这个问题呢? ...
- python 支付宝SDK
python 支付宝SDK代码如下 from datetime import datetime from Crypto.PublicKey import RSA from Crypto.Signatu ...
- python使用stomp连接activemq
一.安装ActiveMQ服务 1. 当使用windows时,安装参考:https://blog.csdn.net/WuLex/article/details/78323811 启动:运行activem ...
随机推荐
- Linux怎么安装vim编译器
我的Linux系统是Ubantu14.04,默认的是vi编译器,现在安装vim编译器 打开终端输入:sudo apt-get install vim-gtk 一般来说就可以了,但是我的提示出现了报错: ...
- Java中Dom解析XML
DOM将整个XML文件加载到内存中,并构建出节点树:应用程序可以通过遍历节点树的方式来解析XML文件中的各个节点.属性等信息:这种方式便于对XML节点的添加修改等,而且解析也很方便,然后它比较耗费内存 ...
- 常用的一些js方法
<input type="text" name="number" onkeyup="floatNum(this)"/> // 数 ...
- FZU - 1688 Binary land
题目链接 Problem 1688 Binary land Accept: 72 Submit: 171Time Limit: 1000 mSec Memory Limit : 3276 ...
- JSON与对象的相互转换
json是一种轻量级的数据格式,(本质为字符串) 低版本可以使用json2.js插件来解决.下载地址:https://github.com/douglascrockford/JSON-js JSON语 ...
- 用贪心算法近似求解 Loading Balance 问题(作业调度的负载均衡)
一,Loading Balance 问题描述:有 m 台相同的机器及 n 个作业,其中 m={M(1),M(2),……M(m)}.n = {J(1),J(2),……J(n)}.每个作业都有一个处理时间 ...
- delimiter 与 存储过程
1.如此执行语句不行,需要在 delimiter IF not EXISTS ( SELECT * FROM information_schema. COLUMNS WHERE table_schem ...
- JDBC preparedStatement分页和统计,批处理和事务
一个类:DriverManager 四个接口:Connection.PreparedStatement .ResultSet.Statement 连接不上数据库出错的原因 1.数据库监听服务的配置不正 ...
- js 获取属性名称
$(function () { myfun(); }) function myfun() { var ...
- HDU 1162 Eddy's picture (最小生成树 prim)
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...