Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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
Python 在S3子文件夹中保存文件_Python_Django_Amazon S3 - Fatal编程技术网

Python 在S3子文件夹中保存文件

Python 在S3子文件夹中保存文件,python,django,amazon-s3,Python,Django,Amazon S3,如果我没有弄错的话,S3中没有真正的文件夹。但似乎可以使用路径保存文件,使其看起来像一个文件夹(在AWS控制台中导航时,文件夹甚至可见) 在Django中有一个FileField,我如何向其附加文件夹名 例如,下面的代码可以工作,但正在放入根存储桶中,很快就会变得杂乱无章。我想把所有这些文件都放在“子文件夹”中 class presentation(models.Model): """ Holds the presentation themselves""" author = m

如果我没有弄错的话,S3中没有真正的文件夹。但似乎可以使用路径保存文件,使其看起来像一个文件夹(在AWS控制台中导航时,文件夹甚至可见)

在Django中有一个FileField,我如何向其附加文件夹名

例如,下面的代码可以工作,但正在放入根存储桶中,很快就会变得杂乱无章。我想把所有这些文件都放在“子文件夹”中

class presentation(models.Model):
    """ Holds the presentation themselves"""
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    file = models.FileField(
        null=False,
        blank=False,
        help_text='PPT or other presentation accessible only to members',
        storage=protected_storage,
    )

protected_storage = storages.backends.s3boto3.S3Boto3Storage(
    acl='private',
    querystring_auth=True,
    querystring_expire=120,  # 2 minutes, try to ensure people won't/can't share
)

将upload_添加到参数并使其指向函数

def my_upload_func(instance, filename):
    return f"test/awesome/{filename}"

file = models.FileField(
        null=False,
        blank=False,
        help_text='PPT or other presentation accessible only to members',
        storage=protected_storage,
        upload_to=my_upload_func
)

如果所说的
子文件夹
,您的意思是
bucket
,那么请将
bucket
参数提到
s3boto3存储
构造函数。工作得很好,非常感谢!但是,如果在upload func中没有使用实例,那么它是做什么的呢?如果您想从表示中派生某种类型的属性,那么实例就是。假设您想将它放在带有f“{instance.author.name}/{filename}”的author's name存储桶中