關於如何申請AWS試用帳號,
建立EC2 instance, S3 bucket,
應該看AWS官網的圖片教學就可以了.
這邊假設已經有一台EC2 centos 6.7 instance, 一個s3 bucket.
那為何要做這件事,
因為S3在容量的使用上明顯事比EC2更划算,
而如果使用API的方式讓程式去讀寫S3的資料,
對既有的程式來說勢必改動期原來讀寫檔的部分.
這工程依程式性質不同可大可小,並且不好轉移其他平台.
所以才會使用掛載的方式.
事前準備
先到AWS/IAM(Identity & Access Management)服務下建立一個使用者帳號,接著把credential載回來,
這個檔案內有兩把Key Access Key Id , Secret Access Key. 後續會用到.
並設定S3FullAccess的權限
接著安裝s3fs所需要的相依套件,s3fs - InstallationNotes.wiki
裡面fuse比較特別,如果你之前已經安裝過了fuse建議這邊先移除,
yum remove fuse fuse* fuse-devel -y
因為s3fs要求fuse 2.8.4以上版本,而centos 6 yum只能裝到2.8.3,如下圖
如果你之前fuse使用make install安裝的話移除方式為 make uninstall
安裝其他相關套件
yum install gcc libstdc++-devel gcc-c++ curl-devel libxml2-devel openssl-devel mailcap -y
下載fuse並安裝
cd /usr/src/
wget https://github.com/libfuse/libfuse/releases/download/fuse_2_9_4/fuse-2.9.3.tar.gz #目前最新到2.9.5, sourceforge已經找不到囉
tar xzf fuse-2.9.3.tar.gz
cd fuse-2.9.3
./configure --prefix=/usr # 預設/usr/local,但在掛載時s3fs會找不到fuse library,我又搞不定,只好裝第二次= ="
make
make install
ldconfig
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/lib64/pkgconfig/
modprobe fuse # 自動處理可載入module
pkg-config --modversion fuse # 檢查版本
ldconfig -v | grep fuse # 檢查library路徑
相依套件安裝完成,下載s3fs並安裝
cd /usr/src/
wget https://s3fs.googlecode.com/files/s3fs-1.74.tar.gz
tar xzf s3fs-1.74.tar.gz
cd s3fs-1.74
./configure --prefix=/usr # 預設/usr/local
make
make install
ldconfig
掛載準備
echo Access Key Id:Secret Access Key > ~/.passwd-s3fs # Access Key Id:Secret Access Key 就是之前從IAM下載的檔案內含資料
chmod 600 ~/.passwd-s3fs
mkdir -p /tmp/cache
mkdir -p /mnt/s3-demo
掛載
s3fs -o use_cache=/tmp/cache bucket_name /mnt/s3-demo
卸載
umount /mnt/s3-demo
開機掛載
把下面這行加到/etc/fstab
s3fs#bucket_name /mnt/s3-demo fuse allow_other,use_cache=/tmp/cache 0 0
reboot後已df -h檢查
出現 s3fs 256 T /mnt/s3-demo字樣即可.
參考
centos 6
How to Mount S3 Bucket on CentOS/RHEL and Ubuntu using S3FS
Mount an Amazon S3 Bucket to a Local Linux File System
centos7
CentOS 7 EC2掛載S3