Dhe*_*dra 90 python amazon-s3 boto amazon-web-services
我想使用python在s3存储桶中复制一个文件.
例如:我有桶名=测试.在存储桶中,我有2个文件夹名称"转储"和"输入".现在我想使用python将文件从本地目录复制到S3"dump"文件夹...任何人都可以帮助我吗?
Fel*_*cia 95
试试这个...
import boto
import boto.s3
import sys
from boto.s3.key import Key
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump'
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY)
bucket = conn.create_bucket(bucket_name,
location=boto.s3.connection.Location.DEFAULT)
testfile = "replace this with an actual filename"
print 'Uploading %s to Amazon S3 bucket %s' % \
(testfile, bucket_name)
def percent_cb(complete, total):
sys.stdout.write('.')
sys.stdout.flush()
k = Key(bucket)
k.key = 'my test file'
k.set_contents_from_filename(testfile,
cb=percent_cb, num_cb=10)
Run Code Online (Sandbox Code Playgroud)
[更新]我不是一个pythonist,所以感谢关于import语句的提示.另外,我不建议在自己的源代码中放置凭据.如果您在AWS内部运行此操作,请使用带有实例配置文件的IAM凭据(http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html),并保持相同的行为你的开发/测试环境,使用AdRoll的全息图(https://github.com/AdRoll/hologram)
vca*_*rel 44
不需要那么复杂:
s3_connection = boto.connect_s3()
bucket = s3_connection.get_bucket('your bucket name')
key = boto.s3.key.Key(bucket, 'some_file.zip')
with open('some_file.zip') as f:
key.send_file(f)
Run Code Online (Sandbox Code Playgroud)
小智 35
我用过它,实现起来非常简单
import tinys3
conn = tinys3.Connection('S3_ACCESS_KEY','S3_SECRET_KEY',tls=True)
f = open('some_file.zip','rb')
conn.upload('some_file.zip',f,'my_bucket')
Run Code Online (Sandbox Code Playgroud)
https://www.smore.com/labs/tinys3/
Bor*_*ris 32
import boto3
s3 = boto3.resource('s3')
BUCKET = "test"
s3.Bucket(BUCKET).upload_file("your/local/file", "dump/file")
Run Code Online (Sandbox Code Playgroud)
Rom*_*rac 23
在具有凭据的会话中将文件上传到 s3。
import boto3
session = boto3.Session(
aws_access_key_id='AWS_ACCESS_KEY_ID',
aws_secret_access_key='AWS_SECRET_ACCESS_KEY',
)
s3 = session.resource('s3')
# Filename - File to upload
# Bucket - Bucket to upload to (the top level directory under AWS S3)
# Key - S3 object name (can contain subdirectories). If not specified then file_name is used
s3.meta.client.upload_file(Filename='input_file_path', Bucket='bucket_name', Key='s3_output_key')
Run Code Online (Sandbox Code Playgroud)
Man*_*hra 13
from boto3.s3.transfer import S3Transfer
import boto3
#have all the variables populated which are required below
client = boto3.client('s3', aws_access_key_id=access_key,aws_secret_access_key=secret_key)
transfer = S3Transfer(client)
transfer.upload_file(filepath, bucket_name, folder_name+"/"+filename)
Run Code Online (Sandbox Code Playgroud)
Sam*_*Nde 13
这是一个三班轮。只需按照boto3 文档中的说明进行操作即可。
import boto3
s3 = boto3.resource(service_name = 's3')
s3.meta.client.upload_file(Filename = 'C:/foo/bar/baz.filetype', Bucket = 'yourbucketname', Key = 'baz.filetype')
Run Code Online (Sandbox Code Playgroud)
一些重要的论据是:
参数:
str
) -- 要上传的文件的路径。str
) -- 要上传到的存储桶的名称。
str
) -- 您要分配给 s3 存储桶中的文件的名称。这可能与文件名或您选择的不同名称相同,但文件类型应保持不变。
注意:我假设您已按照 boto3 文档中最佳配置实践的~\.aws
建议将凭据保存在一个文件夹中。
Piy*_*are 12
这也有效:
import os
import boto
import boto.s3.connection
from boto.s3.key import Key
try:
conn = boto.s3.connect_to_region('us-east-1',
aws_access_key_id = 'AWS-Access-Key',
aws_secret_access_key = 'AWS-Secrete-Key',
# host = 's3-website-us-east-1.amazonaws.com',
# is_secure=True, # uncomment if you are not using ssl
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn.get_bucket('YourBucketName')
key_name = 'FileToUpload'
path = 'images/holiday' #Directory Under which file should get upload
full_key_name = os.path.join(path, key_name)
k = bucket.new_key(full_key_name)
k.set_contents_from_filename(key_name)
except Exception,e:
print str(e)
print "error"
Run Code Online (Sandbox Code Playgroud)
import boto
from boto.s3.key import Key
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
END_POINT = '' # eg. us-east-1
S3_HOST = '' # eg. s3.us-east-1.amazonaws.com
BUCKET_NAME = 'test'
FILENAME = 'upload.txt'
UPLOADED_FILENAME = 'dumps/upload.txt'
# include folders in file path. If it doesn't exist, it will be created
s3 = boto.s3.connect_to_region(END_POINT,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
host=S3_HOST)
bucket = s3.get_bucket(BUCKET_NAME)
k = Key(bucket)
k.key = UPLOADED_FILENAME
k.set_contents_from_filename(FILENAME)
Run Code Online (Sandbox Code Playgroud)
使用 boto3
import logging
import boto3
from botocore.exceptions import ClientError
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
Run Code Online (Sandbox Code Playgroud)
更多信息:- https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html
归档时间: |
|
查看次数: |
170297 次 |
最近记录: |