S3 密钥的内容类型被设置为 'application/octet-stream',即使在 python boto 中将其显式设置为 "text/plain"
Content type of S3 key is being set as 'application/octet-stream' even after explicitly setting it to "text/plain", in python boto
我必须使用 boto 将某个文本文件保存到 S3。我已经使用 "requests" 库获取了文本文件。我编写了以下代码来将文件保存到 S3:
filename = "some/textfile/fi_le.txt"
k = bucket.new_key(filename)
k.set_contents_from_string(r.raw.read())
k.content_type = 'text/plain'
print k.content_type
k = bucket.get_key(filename)
print k.content_type
输出为:
text/plain
application/octet-stream
我应该怎么做才能将文件的 content_type 设置为 'text/plain'?
注意:我正在处理大量此类文件,因此无法在 AWS 控制台中手动设置它。
尝试在调用之前设置内容类型 set_contents_from_string
:
k = bucket.new_key(filename)
k.content_type = 'text/plain'
k.set_contents_from_string(r.raw.read())
之所以必须在之前而不是之后设置它,是因为 set_contents_from_string
方法实际导致将文件写入 S3 - 一旦写入,对本地对象属性的更改将生效'被反映。因此,您首先设置局部属性,然后再写入对象,这样当您设置内容时它们就会被正确写入。
我必须使用 boto 将某个文本文件保存到 S3。我已经使用 "requests" 库获取了文本文件。我编写了以下代码来将文件保存到 S3:
filename = "some/textfile/fi_le.txt"
k = bucket.new_key(filename)
k.set_contents_from_string(r.raw.read())
k.content_type = 'text/plain'
print k.content_type
k = bucket.get_key(filename)
print k.content_type
输出为:
text/plain
application/octet-stream
我应该怎么做才能将文件的 content_type 设置为 'text/plain'?
注意:我正在处理大量此类文件,因此无法在 AWS 控制台中手动设置它。
尝试在调用之前设置内容类型 set_contents_from_string
:
k = bucket.new_key(filename)
k.content_type = 'text/plain'
k.set_contents_from_string(r.raw.read())
之所以必须在之前而不是之后设置它,是因为 set_contents_from_string
方法实际导致将文件写入 S3 - 一旦写入,对本地对象属性的更改将生效'被反映。因此,您首先设置局部属性,然后再写入对象,这样当您设置内容时它们就会被正确写入。