简体   繁体   English

使用python boto将文件上传到S3文件夹

[英]Upload file to S3 folder using python boto

I am trying to upload files from local directory to S3 folder. 我正在尝试将文件从本地目录上传到S3文件夹。 I am able to upload files to S3 bucket but I am unable to upload files to folder within S3 bucket. 我可以将文件上传到S3存储桶,但是无法将文件上传到S3存储桶中的文件夹。

Could any one help? 有谁可以帮忙吗? What am i doing wrong here.. 我在这里做错了..

Here is the code: 这是代码:

import os
import sys 
import boto3
import fnmatch
import pprint
import re
import hashlib

SOURCE_DIR  = '/home/user/Downloads/tracks/'
BUCKET_NAME = 'mybucket'
S3_FOLDER = 'mybucket/folder1/'

client = boto3.client('s3')

s3 = boto3.resource('s3')

def get_md5(filename):
    f = open(filename, 'rb')
    m = hashlib.md5()

    while True:
        data = f.read(10240)
        if len(data) == 0:
            break
        m.update(data)

    return m.hexdigest()

def get_etag(filebase,filepath):
    for item in bucket.objects.all():
        keyfile = S3_FOLDER + filebase
        if(keyfile == item.key):
            md5  = get_md5(filepath)
            etag = item.e_tag.strip('"').strip("'")

            if etag != md5:
                print(filebase + ": " + md5 + " != " + etag)
                return(files_to_upload.append(filepath))
        else:
            return(files_to_upload.append(filepath))

files_to_upload = []
for root, dirnames, filenames in os.walk(SOURCE_DIR):
    for filename in filenames:
        filepath = os.path.join(root, filename)
        get_etag(filename,filepath)

for f in files_to_upload:
    client.put_object(Bucket=BUCKET_NAME, Key=f)

Folders don't really exist in S3. 文件夹在S3中实际上并不存在。 You can prefix the file name (object key) with the something that looks like a folder path. 您可以在文件名(对象键)前面加上类似于文件夹路径的名称。

It's not entirely clear to me what your code is doing with the file paths, but your code needs to be changed to something like this: 对我来说,尚不清楚您的代码对文件路径做了什么,但是您需要将代码更改为以下内容:

for f in files_to_upload:
    key = "my/s3/folder/name/" + f
    client.put_object(Bucket=BUCKET_NAME, Key=key, Body=f)

Note: You weren't passing a Body parameter, so I think your code was just creating empty objects in S3. 注意:您没有传递Body参数,因此我认为您的代码只是在S3中创建空对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM