雲端空間Synology-C2-Object

前言

一提到雲端空間,廣為人知的不外乎是Google Drive、DropBox、OneDrive等等知名大廠的產品,NAS(地端儲存伺服器)則大多常見為Synology群暉所主導,而本篇要介紹的Synology C2 Object則是群暉所提供的服務之一。

在網路上有許多服務經過使用者對網頁進行互動,伺服器背後通常都會有一個以上的資料庫及檔案空間進行儲存,而本篇 Synology C2 Object 就是指檔案存存放空間,只因為它屬於雲端所提供的服務,因此叫做雲端空間。

未來只要有建立好連接的參數,就可以透過API進行呼叫存放、取用,有關檔案空間申請主要目的為取得下列重要參數,以備未來進行檔案空間串接。

  • BUCKET_NAME
  • endpoint_url
  • aws_access_key_id
  • aws_secret_access_key

申請流程

點擊登入

可以綁定Google或者自行申請一組帳號密碼

Fig1.多種登入方式

登入畫面

Fig2.入口畫面

選取C2 Object Storage

Fig3.選擇C2 Object Storage服務

點選開始使用15GB免費空間

Fig4.開始使用

點選服務條款

Fig5.服務條款

建立儲存體

Fig6.建立儲存體
Fig7.建立存取金鑰
Fig8.預設就好

取得金鑰ID與私密金鑰

Fig9.存取金鑰與私密金鑰
  • aws_access_key_id
  • aws_secret_access_key

確認儲存體名稱及端點

Fig9.儲存體名稱及端點
  • BUCKET_NAME
  • endpoint_url

結論

申請完畢之後就可以用Python中Boto3的套件進行檔案空間的儲存、下載了,未來要開發相關網站的時候也多了一個地方可以放置檔案。

開發範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError
import streamlit as st
import pandas as pd

# 請填入你的 bucket 名稱
BUCKET_NAME = 'hankbucket'
# 設定 S3 客戶端
def get_s3_client():
return boto3.client(
's3',
endpoint_url='https://OOOOO.OO.synologyc2.net',
aws_access_key_id='twmOPTcq8MAxXaNVMOOOOOOOOO',
aws_secret_access_key='n7O1Pys3nyp15T5OGOOOOOOO'
)

# 列出所有檔案
def list_files(bucket_name):
s3 = get_s3_client()
try:
response = s3.list_objects_v2(Bucket=bucket_name)
files = response.get('Contents', [])
# print(files)
file_list = []
for file in files:
file_info = {
"File Name": file['Key'],
"Size (bytes)": file['Size'],
"Last Modified": file['LastModified'],
"ETag": file['ETag']
}
file_list.append(file_info)
return file_list
except (NoCredentialsError, PartialCredentialsError) as e:
return str(e)
# 上傳檔案
def upload_file(bucket_name, file_path, object_name):
s3 = get_s3_client()
try:
with open(file_path, 'rb') as f:
s3.upload_fileobj(f, bucket_name, object_name)
return f"Uploaded {object_name} to {bucket_name}"
except Exception as e:
return str(e)
# 上傳檔案並獲取唯一碼
def upload_file_and_get_etag(bucket_name, file_path, object_name):
s3 = get_s3_client()
try:
with open(file_path, 'rb') as f:
response = s3.upload_fileobj(f, bucket_name, object_name)
return response['ETag']
except Exception as e:
return str(e)
# 上傳檔案並顯示唯一碼
def upload_new_file():
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
with open(uploaded_file.name, "wb") as f:
f.write(uploaded_file.getbuffer())
etag = upload_file_and_get_etag(BUCKET_NAME, uploaded_file.name, uploaded_file.name)
st.write(f"Uploaded {uploaded_file.name} with ETag: {etag}")
# 刪除檔案
def delete_file(bucket_name, object_name):
s3 = get_s3_client()
try:
s3.delete_object(Bucket=bucket_name, Key=object_name)
return f"Deleted {object_name} from {bucket_name}"
except Exception as e:
return str(e)

# 列出所有檔案
def list_all_files():
files = list_files(BUCKET_NAME)
if files:
# print(files)
file_list = [{"File Name": file['File Name'],
"Etag": file['ETag'],
"Size (bytes)": file['Size (bytes)'], "Last Modified": file['Last Modified']}
for file in files]
df = pd.DataFrame(file_list)
return df
else:
return pd.DataFrame(columns=["File Name", "ETag"])
# 下載檔案
def download_file(bucket_name, object_name):
s3 = get_s3_client()
try:
with open(object_name, 'wb') as f:
s3.download_fileobj(bucket_name, object_name, f)
return f"Downloaded {object_name} from {bucket_name}"
except Exception as e:
return str(e)
# 下載檔案
def download_selected_file(selected_file):
response = download_file(BUCKET_NAME, selected_file)
st.write(response)

# Streamlit app layout
st.title("Synology C2 Storage Management with S3 API")

st.header("List and Download Files")
files_df = list_all_files()
st.dataframe(files_df)

if not files_df.empty:
selected_file = st.selectbox("Select a file to download", files_df["File Name"].tolist())
if st.button("Download File"):
download_selected_file(selected_file)

st.header("Upload a New File and Get Unique ETag")
upload_new_file()