在s3上设置Fog存储文件的content_type(Set content_type of Fog storage files on s3)
我正在与Fog和Amazon s3合作管理视频和图像文件。 我为我的文件设置content_type时遇到了很多麻烦。
在从控制台工作时,我能够通过并单独更新每个文件的content_type,然后运行save。 但是,当我尝试对特定目录中的所有文件运行更新时,我没有收到错误,但没有任何更新。 我运行了多种不同的方法,都具有相同的基本思想,并且都设置为打印“已保存!” 如果文件保存。 方法运行正常并打印出“已保存!”,但当我返回并检查文件时,content_type仍为零。
这是我正在做的一个例子:
directory.files.each do |f| case f.key.split(".").last when "jpg" f.content_type = "image/jpeg" puts "saved!" if f.save when "mov" f.content_type = "video/quicktime" puts "saved!" if f.save end end
此外,当我通过并单独更新每个文件时,保存工作和content_type得到更新,但数据不会持久。
例如:
file = directory.files.first file.content_type = 'video/quicktime' file.save # returns true file.content_type # returns 'video/quicktime'
但是,当我在AWS中检查文件时,内容类型仍为零。
是否有更好的(持久的)方法来更新Fog s3文件上的content_type? 我觉得我必须以错误的方式解决这个问题。
更新:使用文件#copy方法尝试:
directory.files.each do |f| content_type = case f.key.split(".").last when "jpg" "image/jpeg" when "mov" "video/quicktime" end puts "copied!" if f.copy(f.directory.key, f.key, { 'Content-Type' => content_type }) end
我收到一个错误:
Excon::Errors::BadRequest: Expected(200) <=> Actual(400 Bad Request) from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/middlewares/expects.rb:6:in `response_call' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:355:in `response' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:249:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/core/connection.rb:21:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/storage.rb:506:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/requests/storage/copy_object.rb:33:in `copy_object' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/models/storage/file.rb:93:in `copy' from (irb):14 from /Users/marybethlee/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
I'm working with Fog and Amazon s3 to manage video and image files. I've been running into a lot of trouble with setting the content_type for my files.
When working from the console, I am able to go through and individually update each file's content_type, and then run save. However, when I try to run an update on all of the files within a specific directory, I don't get an error, but nothing gets updated. I've run multiple different methods, all with the same basic idea, and all set to print "saved!" if the file saves. The methods run properly and print out "saved!", but when I go back and check the files, the content_type is still nil.
Here's an example of what I'm doing:
directory.files.each do |f| case f.key.split(".").last when "jpg" f.content_type = "image/jpeg" puts "saved!" if f.save when "mov" f.content_type = "video/quicktime" puts "saved!" if f.save end end
Also, when I go through and individually update each file, the save works and the content_type gets updated, but the data doesn't persist.
For example:
file = directory.files.first file.content_type = 'video/quicktime' file.save # returns true file.content_type # returns 'video/quicktime'
However, when I go check the file in AWS, the content type is still nil.
Is there a better (persistent) way of going about updating content_type on Fog s3 files? I feel like I must be going about this the wrong way.
Update: Tried using the file#copy method:
directory.files.each do |f| content_type = case f.key.split(".").last when "jpg" "image/jpeg" when "mov" "video/quicktime" end puts "copied!" if f.copy(f.directory.key, f.key, { 'Content-Type' => content_type }) end
I got an error:
Excon::Errors::BadRequest: Expected(200) <=> Actual(400 Bad Request) from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/middlewares/expects.rb:6:in `response_call' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:355:in `response' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/excon-0.22.1/lib/excon/connection.rb:249:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/core/connection.rb:21:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/storage.rb:506:in `request' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/requests/storage/copy_object.rb:33:in `copy_object' from /Users/marybethlee/.rvm/gems/ruby-2.0.0-p0@mothership/gems/fog-1.11.1/lib/fog/aws/models/storage/file.rb:93:in `copy' from (irb):14 from /Users/marybethlee/.rvm/rubies/ruby-2.0.0-p0/bin/irb:16:in `<main>'
原文:
最满意答案
相关问答
更多-
SQL中最接近当前日期的日期(先前日期或未来日期)(Date closest to current date in SQL (prior dates or future dates))[2022-05-17]
我仍然不完全理解你的问题,但我希望这给你一个开始,你可以在这里尝试rextester示例 但是对我来说,你需要一个简单的案例陈述: select ITEMNMBR ,case when abs(datediff(day, MINDATE, convert(date,getdate()))) > abs(datediff(day, MAXDATE, convert(date,getdate()))) then 'MINDATE is greater' else 'MAXDATE is great ... -
尝试下面的查询,你错过日期的报价,如“2012-05-20” SELECT * FROM `nfw_users` WHERE DATE(date_join) BETWEEN "2013-05-20" AND "2013-10-20" AND YEAR(date_join) = YEAR(NOW() - INTERVAL 1 YEAR) Try below query and you are missing quotes around date like "2012-05-20" SELECT * F ...
-
一年前在db2中获取日期(Get one year ago date in db2)[2023-12-22]
select date(days(dateCol)-(case when month(dateCol)=2 and day(dateCol)=29 then 366 else 365 end)) from table db2 => values (date(days('28.02.2004')-(case when month(dateCol)=2 and day(dateCol)=29 then 366 else 365 end))) > 2003-02-28 1 record(s) selecte ... -
SELECT YEAR(ASOFDATE) FROM PASOFDATE 编辑:任何情况下,如果你的日期是一个字符串,让我们把它转换成适当的日期格式。 并选择其中的一年。 SELECT YEAR(STR_TO_DATE(ASOFDATE, '%d-%b-%Y')) FROM PSASOFDATE 既然你正在尝试蟾蜍,你可以检查下面的代码: SELECT EXTRACT (TO_DATE(YEAR, 'MM/DD/YY') FROM ASOFDATE) FROM PSASOFDATE; 参考: SELE ...
-
SQL查询 - 确保表中的数据涵盖一年,且没有日期重叠(SQL Query - Ensure data in a table covers a year, with no date overlaps)[2024-02-22]
重叠是相对容易的(但你可能不得不担心时间的“<='vs'<'和'> ='vs'>'): SELECT x1.*, x2.*, "Overlap" FROM tblRentalRates AS x1, tblRentalRates AS x2 WHERE x1.rate_startdate < x2.rate_enddate AND x1.rate_enddate > x2.rate_startdate AND x1.rate_id < x2.date_id -- Avoid rep ... -
从星期和年份获取日期 - 日期必须是一年中的第一天--sql(Get date from week and year - date must be first day of the year - sql)[2023-09-16]
除非我读错了,否则你自己会做得太难。 这样做: declare @year int; set @year = 2017; declare @week int; set @week = 4; select dateadd(week, @week, dateadd(year, @year - 1900, 0)) 结果: 2017-01-29 00:00:00.000 无论工作日如何,内部dateadd()都会为您提供所请求年份(1月1日)的第一天。 如果是星期三,你会得到星期三。 今年是星期天。 外部dat ... -
SQLite没有DATE数据类型。 日期通常存储为字符串(CSV无论如何都没有类型),并且要与SQLite的内置日期函数兼容,必须采用yyyy-mm-dd格式。 在从Excel导出之前,请确保使用此格式格式化所有日期。 SQLite does not have a DATE data type. Dates are typically stored as strings (CSV does not have types anyway), and to be compatible with SQLite's ...
-
你可以用直接逻辑做到这一点; select monto from cupones where (year(fechapago) = year(date() and (month(fechapago) > month(date()) or (month(fechapago) == month(date() and day(fechapago) > day(date()))( or (year(fechapago()) > year(date())) You can do it with straig ...
-
SQL Query从当前日期返回超过一年的日期(SQL Query that returns dates that are over a year ago from the current date)[2023-08-23]
最简单的方法可能是使用date_sub : SELECT table1.time FROM table1 WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR) > table1.time The easiest way would probably be to use date_sub: SELECT table1.time FROM table1 WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR) > ta ... -
你是对的 - SQL不容易识别缺失的数据。 通常的技巧是将您的序列(带缺口)与完整的序列连接起来,并在后面的序列中选择这些元素,而不在数据中使用相应的伙伴。 所以,@本霍夫斯坦建议保持一个永久性的日期表是一个很好的建议 。 简而言之,您可以使用整数表动态创建该日期范围。 假设integers表的第i列的数字至少为0 - 13,并且您的表的日期列名为datestamp : SELECT candidate_date AS missing FROM (SELECT CURRENT_DATE + ...