通过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的更多相关文章

  1. 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例

    问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...

  2. Java通过aws java sdk在AWS S3上进行操作客户端

    有两种方式可以安装aws java sdk:方式一:可以在这个网站上下载适用于 Java 的 AWS 开发工具包https://aws.amazon.com/cn/sdk-for-java/点击网页右 ...

  3. MinIO Python Client SDK 快速入门指南

    官方文档地址:http://docs.minio.org.cn/docs/master/python-client-quickstart-guide MinIO Python Client SDK提供 ...

  4. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  5. Python中HTTPS连接

    permike 原文 Python中HTTPS连接 今天写代码时碰到一个问题,花了几个小时的时间google, 首先需要安装openssl,更新到最新版本后,在浏览器里看是否可访问,如果是可以的,所以 ...

  6. Ubuntu 14.04 配置vsftpd实现FTP服务器 - 通过FTP连接AWS

    测试主机:亚马逊AWS EC2 系统:Ubuntu 14.04 想用AWS来做服务器玩,结果发现其不能像简单使用阿里云服务器那样用ftp连接,反正也不熟悉ftp服务器搭建,那就乘这个机会学习一下如何利 ...

  7. 亚马逊的PuTTY连接AWS出现network error connection refused,终极解决方案。

    使用PuTTY连接AWS的时候,一直出现network error connection refused.百度了这个问题,大家都说是SSH要设置成22.但是我已经设置过了,为什么还是遇到这个问题呢? ...

  8. python 支付宝SDK

    python 支付宝SDK代码如下 from datetime import datetime from Crypto.PublicKey import RSA from Crypto.Signatu ...

  9. python使用stomp连接activemq

    一.安装ActiveMQ服务 1. 当使用windows时,安装参考:https://blog.csdn.net/WuLex/article/details/78323811 启动:运行activem ...

随机推荐

  1. 日志的使用-log4j

    1,首先添加对log4j-core-2.6.2.jar,log4j-api-2.6.2.jar的引用. https://files.cnblogs.com/files/renjing/log4j.zi ...

  2. 二、存储管理器--SDRAM

    2.1 硬件结构 2.1.1 硬件框图 CPU 通过存储管理器来读取 SDRAM 网卡 等外部设备,CPU不管外部设备是怎么样的,只是读存储管理器中的地址 CPU从0x30000000地址读取数据. ...

  3. u-boot移植(五)---代码修改---时钟修改、SDRAM

    最开始已经建立了新单板以及配置文件,现在就需要做的是代码的修改,配置成适合目标板使用的u-boot. 一.时钟修改 在代码流程分析中,我们知道,系统的启动是: 设置 CPU 为管理员模式 关闭看门狗 ...

  4. _vimrc(VimScript脚本语言学习)

    Windows下 syntax on "高亮 "缩进 set cindent "set cin set smartindent "set si set auto ...

  5. Django学习手册 - 正则URL路由配置/路由分发

    ############################################### 总结: 一.url路由配置: 方式一:(通过url链接get获取) 方式二:(url路由匹配方式获取-拓 ...

  6. Apple Watch 开发详解

    Apple Watch 开发详解 Apple Watch现在对于第三方开发者来说更多的还是一块额外的屏幕.暂时WatchKit没有能给出足够的接口.现在Watch App的主要运算逻辑需要依赖iPho ...

  7. weblogic对JSP预编译、weblogic读取JSP编译后的class文件、ant中weblogic.jspc预编译JSP

    我们都知道在weblogic中JSP是每次第一次访问的时候才会编译,这就造成第一次访问某个JSP的时候性能下降,有时候我们也希望JSP被编译成class然后打包在jar中实现隐藏JSP的功能,下面介绍 ...

  8. python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib

    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multib ...

  9. aliyun添加数据盘后的物理分区和lvm逻辑卷两种挂载方式

    一.普通磁盘分区管理方式 1.对磁盘进行分区 列出磁盘 # fdisk -l # fdisk /dev/vdb Welcome to fdisk (util-linux 2.23.2). Change ...

  10. iptables实现网络防火墙及地址转换

    iptables主机防火墙功能及常用命令 FSM:Finite State Machine 有限状态机 客户端:closed -->syn_sent -->established --&g ...