SlideShare una empresa de Scribd logo
1 de 21
S3からフォルダのようなkeyが返
ってくる
ユニークビジョン株式会社
中野 隆行
発端
以前担当していた、とある受託システムでのこと。
このシステムはユーザーが画像をアップロードする機能があった。
アップロードされた画像はとあるバケットに
user_images/{user_id}/{image_id}
という key で保存される。
ちなみに開発言語は ruby
ある時、保存されているユーザー画像の key の一覧が欲しくなりました。
require 'aws-sdk'
client = Aws::Credentials.new(
region: 'REGION',
access_key_id: 'YOUR_ACCESS_KEY',
secret_access_key: 'YOUR_SECRET_KEY'
)
client.list_objects(:bucket => 'some-bucket', :prefix => 'user_images/').contents.each do |object|
p object.key
end
得られた結果はこんな感じ
user_images/
user_images/1/1
user_images/1/2
user_images/2/3
当時の私の考え
「ユーザー 1 が 画像 1, 2 を持っていて、」
「ユーザー 2 が 画像 3 を持っている。」
「へー、フォルダのパスも key として取得してくるんだー。」
「・・・。」
「邪魔だし削除して使わなきゃ」
フォルダのkeyが取れるなら
user_images/
user_images/1/
user_images/1/1
user_images/1/2
user_images/2/
user_images/2/3
どうしてこうならないのだろう?
もしかして単純にフォルダの key が取得できているわけではないのか?
というわけで「S3 フォルダ」で検索!
出てくる記事はこれ
Amazon S3における「フォルダ」という幻想をぶち壊し、その実体を明らかにす
る
記事によると
> Amazon S3には実はフォルダという概念は無い
> Amazon S3の基礎技術は、単純なKVS(Key-Value型データストア)でしかあ
りません。
つまり、「hoge」 とだけ書いたテキストファイルをとあるバケットに foo/bar.txt
という key でアップロードした場合、その実態は。
バケット
└ foo
└ bar.txt <- "hoge"
こんなファイル構造ではなく。
key value
foo/bar.txt hoge
ただ単純にこのような key value で保存されているだけなのだという。
なので、bar.txt を削除した場合 S3 的には foo/bar.txt を削除したことになるので
foo というファイルもコンソールから消える。
そして、フォルダの様に見えるものは key の / を判断して
階層構造のように見せてくれているだけ。
なるほどなー。フォルダなんてものは S3 に無いんだ。
ん?じゃあ何だこのボタン。
やっと本題
先程の「S3 にフォルダは無い」という話に真っ向から立ち向かう
コンソールに居座るこの
これこそが当初の目的である
なんか返ってきたフォルダっぽい key の原因なのです。
フォルダ(のようなもの)の正体
この を押して実行すると、
指定したフォルダ名を key にした size 0 のファイルが作成されるようです。
これが list_objects に引っかかる。
つまり、この場合
user_images/ はコンソールから で作成されたファイルということ。
うーん、たしかにそんなことをしたかと聞かれると否定できないぞ!?
user_images/
user_images/1/1
user_images/1/2
user_images/2/3
そして、実際には回収されなかった赤字のファイル(っぽいもの)の key
これは user_images/1/1 や user_images/2/3 はシステムからアップロードされたため
表示されないのである。
user_images/
user_images/1/
user_images/1/1
user_images/1/2
user_images/2/
user_images/2/3
結論
コンソールから作成したフォルダ(っぽいもの) が list_objects で回収される
コンソールから作成したフォルダ(っぽいもの)と ファイルのアップロードででき
た見せかけのフォルダ(っぽいもの) が混在すると list_objects が非常に使いづら
くなる。
少なくとも、同じ階層のフォルダ(っぽいもの)の作成方法は統一しよう。
追記
どうもフォルダっぽく見えるファイルは key が / で終わるので、
これでフィルタリングできるかもしれない。
そもそも、これだってファイルなのだから delete_object とかなんとか言って
削除すれば解決なのかも。
おまけ
せっかくなので で出来たファイルを取得。
key が取れてるのだから取れるだろう。
obj = client.get_object(:bucket => 'some-backet', :key => 'baz/')
p obj.content_length # => 0
p obj.body # => #<StringIO:0x00007fbeddc347d8>
p obj.body.read # =>
おまけ2
じゃあ空ファイルアップしたらどうなるのか?
client.put_object(
:bucket => 'some-backet',
:key => 'hoge/',
:body => ""
)
適当に空文字でも送りつけてみるか。
出来た。
開くと「このパスにオブジェクトはありません。」の表示。
使いみちは謎。
では
一緒に働く仲間を
募集しています!
<募集職種>
【エンジニア】
⮚ 凄腕の技術者集団!Belugaシリーズの開発
⮚ レベルアップしたいエンジニア募集!!
【ディレクター】
⮚ ソーシャルメディア統合管理ツール
「Beluga」の企画、マネジメント
⮚ SE・プログラマーなどシステム開発、
ディレクション経験者歓迎!!
エントリーお待ちしております!
募集媒体・・・・
⮚ Green、paiza、Wantedly
採用情報はTwitter
(@uv_recruit)を
ご確認ください!
UVリクルートページは
こちら🐬

Más contenido relacionado

Destacado

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Destacado (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

A key like_a_folder_is_returned_from_s3