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 ...
随机推荐
- java中equals和compareTo的区别---解惑
大多转载自 百度知道,个人整理以便日后阅读. value1.compareTo(value2) == 0 value1.equals(value2) equals的效率高些,compareTo其实就是 ...
- python---redis的python使用
set以及相关: r.set("foo","bar") print(r.get("foo"))#b'bar' #在Redis中设置值,默认, ...
- python---tornado初识(2)实现登录和发布文章
# coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python import tornado.i ...
- python---tornado初识(1)
# coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python import tornado.i ...
- vue 中this指向遇到的坑
vue中的this指向问题 如果方法中没有使用箭头函数,记得把this赋值给另一个变量再使用.
- java查看当前项目所有线程列表界面【转】
java查看当前项目所有线程列表界面 1.TestThread(测试类) package com.testdemo.pcis.isc.job.king.panel; public class Test ...
- spring @Entity @Table
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; impor ...
- python遍历数组获取下标
代码 通过枚举实现 a = [,,,,,] for i,j in enumerate(a): print i,j 结果
- DotNetBar TextBoxDropDown响应按键事件
textBoxDropDownHelp.TextBox.KeyDown += new KeyEventHandler(textBoxDropDownHelp_KeyDown); private voi ...
- 日历控件QCalendarWidget
样式: