Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon s3 为AWS S3生成index.html_Amazon S3_Boto3 - Fatal编程技术网

Amazon s3 为AWS S3生成index.html

Amazon s3 为AWS S3生成index.html,amazon-s3,boto3,Amazon S3,Boto3,我试图在ASWS3上模拟我的bucket的目录列表。目前,我正在本地创建“index.html”,如下所示: for root, dirs, files in os.walk(job_dir): objects = [] for obj in dirs+files: m_time_epoch = os.stat(os.path.join(path,obj)).st_mtime mtime = datetime.fromtimestamp(

我试图在ASWS3上模拟我的bucket的目录列表。目前,我正在本地创建“index.html”,如下所示:

    for root, dirs, files in os.walk(job_dir):
    objects = []

    for obj in dirs+files:
        m_time_epoch = os.stat(os.path.join(path,obj)).st_mtime
        mtime = datetime.fromtimestamp(m_time_epoch).strftime('%c')
        size = os.stat(os.path.join(path,obj)).st_size
        type = 'dir' if os.path.isdir(os.path.join(path,obj)) else 'file'
        objects.append({'name': obj,
                        'mtime': mtime,
                        'size': size,
                        'type': type})

    generate_index(objects, dest_path)
然后将其与目标路径(bucket URL)一起传递给一个函数,该函数将使用jinja模板创建“index.html”

有更好的方法吗?不过,我希望避免使用JavaScript。我在谷歌上搜索了一下,但到目前为止还没有找到一个优雅的解决方案

使用boto3 python客户端的“os.walk”最简单的替代方案是什么

我发现了一些片段,例如:

但难道没有更简单的解决办法吗

谢谢…

我建议在boto3中使用该方法

import boto3

s3 = boto3.client('s3')

paginator = s3.get_paginator('list_objects_v2')

response_iterator = paginator.paginate(
    Bucket='MyBucket'
)

objects = []
for response in response_iterator:
    for r in response['Contents']:
        print("File is called {}".format(r['Key']))

在遍历bucket中的对象时,您可以构建一个对象,并将其传递给Jinja模板,以创建
index.html
page

,您只需使用。除了获取目录列表之外,您的实际目标是什么?嗨,实际目标是将index.html放入每个目录(甚至是空的)。使用os.walk的本地方式可以很好地工作(然后使用aws cli进行同步),但是在上传对象元数据之后,我还想使用对象元数据。因此,我需要os.walk boto3替代方案。当我在电脑前时,我将尝试@tkwargs建议的方法,但不确定该方法将如何区分文件和目录。玩了一点“公共前缀”,但到目前为止还没有想出简单的解决方案…谢谢你的回答。如果我需要像在os.walk中那样一步一步地浏览每个目录,我应该使用例如split()来处理输出,还是有更好的方法?S3是键/值存储,即它实际上没有目录和子目录的概念。AWS控制台提供了在目录和子目录中单击的体验,但在后台它是简单的键/值存储。有关更多详细信息,请查看。如果键的组织方式有很多逻辑,您可以使用
split()
功能,或者多次调用
list\u objects\u v2
,但在参数页中添加一个
前缀
,我知道这一点。我可能已经知道使用“列表对象”时出现问题的原因。看起来“awss3sync”命令将只在bucket中放入带有前缀的对象(预期)。我还将尝试使用+“/”创建一个“dict对象”,看看它是否有助于我的事业。稍后将更新此问题。再次感谢您的时间…最后,我坚持用当地的方式,但接受答案作为替代方案。一个简单的问题。如果我使用“资源”而不是“客户机”,我还需要分页器吗?