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 ...
随机推荐
- python---tornado初识(2)实现登录和发布文章
# coding:utf8 # __author: Administrator # date: 2018/3/6 0006 # /usr/bin/env python import tornado.i ...
- Study 5 —— CSS概述
CSS(Cascading Style Sheet)称为层叠样式表,也可以称为CSS样式表或样式表,其文件扩展名为.css,CSS是用于增强或控制网页样式,并允许将样式信息与网页内容分离的一种标记性语 ...
- MySQL与宿主Linux之间交互式执行命令
在MySQL里面执行Linux的命令并返回结果 system commands root@localhost 11:36:23> system cal March 2017 Su Mo Tu W ...
- Python - Scrapy 框架
Scrapy 是采用Python 开发的一个快速可扩展的抓取WEB 站点内容的爬虫框架.Scrapy,Python开发的一个快速,高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构 ...
- Nginx Server 配置
http { include mime.types; // 主模块:实现对配置文件包含的文件设定,可以减少主配置文件的复杂度: default_type application/octet-strea ...
- luogu P1641 [SCOI2010]生成字符串
传送门 代码极短 \(O(n^2)\)dp是设\(f_{i,j,k}\)表示前\(i\)位,放了\(j\)个1,后面还可以接着放\(k\)个0的方案,转移的话,如果放0,\(k\)就要减1,反之放了1 ...
- Maven打包编译找不到com.sun.crypto.provider.SunJCE类
Maven配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>mav ...
- python - format函数 /class内置format方法
format函数 # format函数 # 用于字符串格式化 # 基本用法: # 方式一:(位置方式) x = "{0}{1}{2}".format(1,2,3) print('1 ...
- Django CSRF提交遇见的问题
简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功能有 ...
- bootstrap-table插件数据加载方式
data-url 直接使用data-url在table标签中定义 使用load方法加载数据 $(“#finishingTask”).bootstrapTable(‘load’,data); //dat ...