物件鎖定
「物件鎖定」會保留電子記錄,並透過確保個別物件版本以 WORM (一寫多讀)、不可擦寫及不可重寫的方式儲存,來維護資料完整性。 在指定日期或移除任何合法保留之前,都會強制執行此原則。
為何使用物件鎖定?
Object Lock 透過對客戶的備份、災難回復及網路備援工作負載施行資料不可變性,來協助客戶控管資料保留及保留需求。
「物件鎖定」可確保 任何人都無法刪除資料,且 無法暫停物件上的保留。 在鎖定具有保留期的物件之前,請仔細閱讀文件。
使用「物件鎖定」時,您有責任確保符合您 (您的組織) 在長期保留及儲存資料時可能遵守的任何法規。
當使用 Object Lock 時,您有責任確保您的 IBM Cloud 帳戶依據 IBM Cloud 政策和指引保持良好狀態,只要資料有保留期限。 如需相關資訊,請參閱「IBM Cloud 服務」條款。
術語
有兩種方式使用「物件鎖定」來保護資料: 保留期間 及 合法保留。
- 保留期間 定義無法修改或刪除物件的時間範圍。
- 合法保留 也會防止變更物件,但只會保留到明確提升為止。
可以使用這些參數的任何組合-物件版本可以有一個、兩個或都沒有。
保留截止日期 (保留期間)
如果您需要在固定時間量內保護物件版本,則需要指定 保留截止日期,以決定無法變更的期間。 在過了此日期之後,可以刪除物件版本 (假設物件版本沒有合法保留)。
新物件的保留期間可以繼承自儲存區上設定的預設值,也可以在寫入物件時透過指定 保留截止日期來明確定義。
當您使用儲存區預設值時,未指定「保留截止日期」。 相反地,您可以指定持續時間 (以天或年為單位),在此期間,應該保護儲存區中放置的每個物件版本。 當您將物件放入儲存區時,會透過將指定的持續時間新增至物件寫入時間,來計算物件版本的「保留截止日期」。
如果您在儲存區中放置物件版本的要求包含明確保留模式及「保留至日期」,則那些設定會置換該物件版本的任何儲存區預設值。
與所有其他「物件鎖定」設定一樣,「保留截止日期」會套用至個別物件版本。 單一物件的不同版本可以具有不同的保留模式及期間。
假設物件在 90 天保留期的 60 天內,且您以相同的名稱及兩年保留期來改寫該物件。 作業將成功,並建立具有兩年保留期的物件新版本。 同時,在 30 天之後,原始版本即可刪除。
延長保留期
若要延長物件的保留期間,只需傳送要求來設定新的、較長的保留期間。 假設要求者具有 cloud-object-storage.object.put_object_lock_retention
及 cloud-object-storage.object.put_object_lock_retention_version
動作,則舊值將改寫為新的值。
合法保留
合法保留 類似於保留期間,因為它會防止改寫或刪除物件版本。 不過,合法保留更有彈性,且沒有已定義的時間元件。 相反地,它們只會維持有效,直到移除為止。 具有 cloud-object-storage.object.put_object_lock_legal_hold
及 cloud-object-storage.object.put_object_lock_legal_hold_version
動作的任何使用者都可以自由放置及移除合法保留。
合法保留的額外好處是充當對物件套用無限期保留的方法。
合法保留和保留期獨立運作。 合法保留對保留期沒有影響,反之亦然。
想像具有合法保留及保留期間的物件。 當保留期結束時,物件版本會保持受保護狀態,直到移除合法保留為止。 如果您在物件版本受到保留期間的限制時移除合法保留,則在保留期間完成之前,它會保持受保護。
在保留期到期並移除任何相關聯的合法保留之前,無法刪除以保留期鎖定及儲存的物件。
不支援使用控管模式鎖定物件。
開始使用物件鎖定
若要開始使用,有一些必要條件:
- 您將需要儲存區上的
Writer
或Manager
平台角色,或已指派適當動作 (例如cloud-object-storage.bucket.put_object_lock_configuration
) 的自訂角色。 - 必須啟用物件版本化
- 您將需要使用「標準」定價方案,如需詳細資料,請參閱 定價。
- 您需要挑選支援「物件鎖定」的區域,如需詳細資料,請參閱 整合式服務。
- 支援預設保留期間上限 100 年 (或 36500 天)。
- 使用主控台時,除了天數或年數之外,也可以設定「保留截止日期」(以月為單位)。
物件 無法減少的保留期間。 如果您使用預設保留來進行驗證測試,請使用較低持續時間 (例如 1 天) 作為預設保留,視需要將其增加至您想要的設定。
建立並設定新儲存區以與物件鎖定搭配使用
- 導覽至您想要的 Object Storage 實例,並搭配使用 建立儲存區 與 自訂儲存區選項
- 根據您的使用案例需求,輸入必要的儲存區配置詳細資料
- 導覽至 物件版本化 區段,並將它設為 已啟用
- 尋找 Immutability,並在「物件鎖定」下按一下 新增
- 將物件鎖定設為 已啟用
- 選擇性地設定預設保留期間。
- 按一下「儲存」
- 繼續進行其餘配置設定,然後按一下 建立儲存區
在現有儲存區上啟用物件鎖定:
可以設定儲存區以供「物件鎖定」使用,如下所示:
- 導覽至儲存區 配置 區段
- 按一下 物件版本化
- 在「物件版本化」區段中,按一下 編輯,將配置選項設為 已啟用 及 儲存
- 導覽至 物件鎖定 區段,按一下 新增
- 將 物件鎖定 設為 已啟用
- 選擇性地設定預設保留期間。
- 按一下儲存
將保留截止日期或法定保留新增至物件
- 導覽至具有目標物件的儲存區
- 切換 顯示版本
- 跳至目標版本的詳細資料
- 新增保留期間及/或切換合法保留。
使用物件鎖定來進行企業永續和災難回復
「物件鎖定」可用來在勒索軟體攻擊時提供服務的連續性,因為受保護資料無法修改或毀損。
一致性和資料完整性
雖然 IBM Cloud Object Storage 為所有資料 IO 作業提供了強大的一致性,但水桶組態最終會保持一致。 在儲存區上啟用、修改或刪除預設保留期間之後,配置可能需要一些時間才能在系統之間傳播。 物件上的作業 (例如新增合法保留) 會立即一致。
使用情形和會計
已鎖定物件 (及其版本) 提供使用情形,就像任何其他資料一樣,只要物件仍以保留期鎖定,您將負責 使用成本。
互動
根據您的使用案例需求,「物件鎖定」可以與數個物件儲存體特性一起使用。
版本化
啟用版本化 是啟用「物件鎖定」的必要條件。 如果使用 x-amz-bucket-object-lock-enabled
標頭建立儲存區,則會自動啟用版本化。
刪除版本化物件會建立 刪除標記。 物件可能看似已刪除,但如果物件受到保護,則無法刪除受保護的版本。 刪除標記本身不受保護。
抄寫
物件鎖定 無法在來源儲存區 上用於抄寫,只能在目的地上使用。 將指派預設保留期給物件。
金鑰管理系統
受保護物件將使用儲存區的根金鑰進行加密。 在儲存區上啟用「物件鎖定」時,只要相關聯的儲存區已啟用「物件鎖定」,就會保護 Key Protect 或 Hyper Protect Crypto Services 所管理的根金鑰免於刪除。 這可防止加密解構受保護物件。
生命週期配置
可以啟用生命週期原則,保存已鎖定的物件,但自然無法啟用保留或合法保留下 使物件到期 的那些生命週期原則 (儲存區中未受保護的物件仍然可以過期)。
Immutable Object Storage
「物件鎖定」是使用 Immutable Object Storage時可用的保留原則的替代方案。 由於「物件鎖定」需要啟用版本化,且 Immutable Object Storage 與版本化不相容,因此無法在相同儲存區上同時啟用兩個 WORM 解決方案。 在「服務實例」中可以混合使用儲存區,每一個都使用 Immutable Object Storage 或「物件鎖定」。
物件標記
在受保護物件上新增或修改標籤沒有任何限制。
其他互動
搭配使用「物件鎖定」與其他 Object Storage 特性 (例如設定 CORS 原則、設定 IP 防火牆或條件型限制、儲存區配額或 Code Engine) 時,應該不會有不利的互動。
IAM 動作
有新的 IAM 動作與「物件鎖定」相關聯。
IAM 動作 | 角色 |
---|---|
cloud-object-storage.bucket.get_object_lock_configuration |
管理員、撰寫者、讀者 |
cloud-object-storage.bucket.put_object_lock_configuration |
管理員、撰寫者 |
cloud-object-storage.object.get_object_lock_retention |
管理員、撰寫者、讀者 |
cloud-object-storage.object.put_object_lock_retention |
管理員、撰寫者 |
cloud-object-storage.object.get_object_lock_retention_version |
管理員、撰寫者、讀者 |
cloud-object-storage.object.put_object_lock_retention_version |
管理員、撰寫者 |
cloud-object-storage.object.get_object_lock_legal_hold |
管理員、撰寫者、讀者 |
cloud-object-storage.object.put_object_lock_legal_hold |
管理員、撰寫者 |
cloud-object-storage.object.get_object_lock_legal_hold_version |
管理員、撰寫者、讀者 |
cloud-object-storage.object.put_object_lock_legal_hold_version |
管理員、撰寫者 |
請注意,具有「撰寫者」角色的使用者能夠讓物件在許多年 (可能是千年) 無法刪除。 請小心,並考量製作不容許大部分使用者設定「保留截止日期」的自訂角色。
Activity Tracker 事件
「物件鎖定」會產生其他事件。
cloud-object-storage.bucket-object-lock.create
cloud-object-storage.bucket-object-lock.read
cloud-object-storage.object-object-lock-legal-hold.create
cloud-object-storage.object-object-lock-legal-hold.read
cloud-object-storage.object-object-lock-retention.create
cloud-object-storage.object-object-lock-retention.read
對於 cloud-object-storage.bucket-object-lock.create
事件,下列欄位提供額外資訊:
欄位 | 說明 |
---|---|
requestData.object_lock_configuration.enabled |
指出已在儲存區上啟用物件鎖定 |
requestData.object_lock_configuration.defaultRetention.mode |
指出 COMPLIANCE mode is active- GOVERNANCE mode is not yet supported. |
object_lock_configuration.defaultRetention.years |
預設保留期間 (以年為單位)。 |
object_lock_configuration.defaultRetention.days |
預設保留期間 (以天為單位)。 |
只會呈現 object_lock_configuration.defaultRetention.years
或 object_lock_configuration.defaultRetention.days
,但不能同時呈現兩者。
對於受保護物件上的作業,可能存在下列欄位:
欄位 | 說明 |
---|---|
requestData.object_lock_protection.legal_hold |
指出對物件版本有效的合法保留。 |
requestData.object_lock_protection.retention.mode |
指出物件版本上的 COMPLIANCE 模式處於作用中-尚未支援 GOVERNANCE 模式。 |
requestData.object_lock_protection.retention.retain_until_date |
指出物件版本適合刪除的日期。 在此日期之後,物件不再根據保留日期來刪除受保護的物件。 |
REST API 範例
下列範例使用 cURL 顯示以方便使用。 環境變數用來代表使用者特定的元素,例如 $BUCKET
、$TOKEN
及 $REGION
。 請注意,$REGION
也會包含任何網路類型規格,因此使用專用網路將要求傳送至 us-south
中的儲存區需要將變數設為 private.us-south
。
在儲存區上啟用物件鎖定
「物件鎖定」配置在要求內文中以 XML 形式提供。 新要求將改寫儲存區上存在的任何現有抄寫規則。
「物件鎖定」配置必須包含一個規則。
標頭 | 類型 | 說明 |
---|---|---|
Content-MD5 |
字串 | 必要:有效負載的 base64 編碼 128 位元 MD5 雜湊,用來作為完整性檢查,以確保未在傳輸中變更有效負載。 |
要求的內文必須包含具有下列綱目的 XML 區塊:
元素 | 類型 | 子項 | 上代 | 限制 |
---|---|---|---|---|
ObjectLockConfiguration |
容器 | ObjectLockEnabled , Rule |
無 | 必要 限制 1。 |
ObjectLockEnabled |
字串 | 無 | ObjectLockConfiguration |
必要 唯一有效值為 Enabled (區分大小寫)。 |
Rule |
容器 | DefaultRetention |
ObjectLockConfiguration |
限制 1 |
DefaultRetention |
容器 | Days , Mode , Years |
Rule |
限制 1。 |
Days |
整數 | 無 | DefaultRetention |
您要針對預設保留期間指定的天數。 無法與 Years 結合。 |
Mode |
字串 | 無 | DefaultRetention |
目前僅支援 COMPLIANCE (區分大小寫)。 |
Years |
整數 | 無 | DefaultRetention |
您要為預設保留期指定的年數。 無法與 Days 結合。 |
此範例將保留任何新物件至少 30 天。
curl -X "PUT" "https://$BUCKET.s3.$REGION.cloud-object-storage.appdomain.cloud/?object-lock" \
-H 'Authorization: bearer $TOKEN' \
-H 'Content-MD5: exuBoz2kFBykNwqu64JZuA==' \
-H 'Content-Type: text/plain; charset=utf-8' \
-d $'<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<ObjectLockEnabled>Enabled</ObjectLockEnabled>
<Rule>
<DefaultRetention>
<Days>30</Days>
<Mode>COMPLIANCE</Mode>
</DefaultRetention>
</Rule>
</ObjectLockConfiguration>'
成功要求會傳回 200
回應。
檢視水桶的物件鎖設定
curl -X "GET" "https://$BUCKET.s3.$REGION.cloud-object-storage.appdomain.cloud/?object-lock" \
-H 'Authorization: bearer $TOKEN'
這會傳回具有適當綱目的 XML 回應主體:
<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<ObjectLockEnabled>string</ObjectLockEnabled>
<Rule>
<DefaultRetention>
<Days>30</Days>
<Mode>COMPLIANCE</Mode>
</DefaultRetention>
</Rule>
</ObjectLockConfiguration>
新增或延長物件的保留期間
「物件鎖定」配置在要求內文中以 XML 形式提供。 新要求將改寫物件上呈現的任何現有抄寫規則,前提是 RetainUntilDate
在未來比現行值更遠。
標頭 | 類型 | 說明 |
---|---|---|
Content-MD5 |
字串 | 必要:有效負載的 base64 編碼 128 位元 MD5 雜湊,用來作為完整性檢查,以確保未在傳輸中變更有效負載。 |
您可以選擇性地指定要套用 RetainUntilDate
的版本。
選用的查詢參數
參數 | 必要? | 類型 | 說明 |
---|---|---|---|
versionID |
選用 | 字串 | 版本 ID。 |
要求的內文必須包含具有下列綱目的 XML 區塊:
元素 | 類型 | 子項 | 上代 | 限制 |
---|---|---|---|---|
Retention |
容器 | Mode , RetainUntilDate |
無 | 必要 限制 1。 |
Mode |
字串 | 無 | Retention |
目前只支援 必要 COMPLIANCE (區分大小寫)。 |
RetainUntilDate |
字串 | 無 | Retention |
必要 在 ISO8601 日期時間格式中可刪除物件之前的日期。 |
此範例將保留任何新物件至少到 2023 年 3 月 12 日為止。
curl -X "PUT" "https://$BUCKET.s3.$REGION.cloud-object-storage.appdomain.cloud/?retention" \
-H 'Authorization: Bearer $TOKEN' \
-H 'Content-MD5: fT0hYstki6zUvEh7abhcTA==' \
-H 'Content-Type: text/plain; charset=utf-8' \
-d $'<Retention>
<Mode>COMPLIANCE</Mode>
<RetainUntilDate>2023-03-12T23:01:00.000Z</RetainUntilDate>
</Retention>'
成功要求會傳回 200
回應。
如果 RetainUntilDate
值未超出任何現有值,則作業會失敗,並產生 403 Access Denied
。
新增或移除物件的合法保留
「物件鎖定」配置在要求內文中以 XML 形式提供。 新要求將改寫物件上呈現的任何現有抄寫規則,前提是 RetainUntilDate
在未來比現行值更遠。
標頭 | 類型 | 說明 |
---|---|---|
Content-MD5 |
字串 | 必要:有效負載的 base64 編碼 128 位元 MD5 雜湊,用來作為完整性檢查,以確保未在傳輸中變更有效負載。 |
要求的內文必須包含具有下列綱目的 XML 區塊:
元素 | 類型 | 子項 | 上代 | 限制 |
---|---|---|---|---|
legal-hold |
容器 | Status |
無 | 限制 1。 |
Status |
字串 | 無 | legal-hold |
支援的值為 ON 或 OFF (區分大小寫) |
此範例將保留任何新物件至少到 2023 年 3 月 12 日為止。
curl -X "PUT" "https://$BUCKET.s3.$REGION.cloud-object-storage.appdomain.cloud/?legal-hold&versionId=$VERSION_ID" \
-H 'Authorization: Bearer $TOKEN' \
-H 'Content-MD5: FMh6GxizXUBRaiDuB0vtgQ==' \
-H 'Content-Type: text/plain; charset=utf-8' \
-d $'<legal-hold>
<Status>ON</Status>
</legal-hold>'
成功要求會傳回 200
回應。
SDK 範例
下列範例使用 IBM COS SDK for Python、Node.js及 Go,以及 Terraform Script,雖然物件版本化的實作應該與任何容許設定自訂端點的 S3-compatible 程式庫或工具完全相容。 使用協力廠商工具需要 HMAC 認證 來計算 AWS V4 簽章。
Python
使用 IBM COS SDK for Python 啟用「物件鎖定」可以使用 低階用戶端 語法來完成。
使用用戶端:
import ibm_boto3
from ibm_botocore.client import Config
from ibm_botocore.exceptions import ClientError
from datetime import datetime, timedelta
import time
# Create new bucket with Object Lock enabled.
def create_bucket_with_objectlock(bucket_name):
cos_cli.create_bucket(
Bucket=bucket_name,
ObjectLockEnabledForBucket=True,
)
print("Bucket: {0} created with objectlock enabled".format(bucket_name))
def objectlock_configuration_on_bucket(bucket_name):
# Putting default retenion on the COS bucket.
default_retention_rule = {'DefaultRetention': {'Mode': 'COMPLIANCE', 'Years': 1}}
object_lock_config = {'ObjectLockEnabled': 'Enabled', 'Rule': default_retention_rule}
cos_cli.put_object_lock_configuration(Bucket=bucket_name, ObjectLockConfiguration=object_lock_config)
# Reading the objectlock configuration set on the bucket.
response = cos_cli.get_object_lock_configuration(Bucket=bucket_name)
print("Objectlock Configuration for {0} =>".format(bucket_name))
print(response.ObjectLockConfiguration)
def upload_object(bucket_name,object_name,object_content):
cos_cli.put_object(
Bucket=bucket_name,
Key=object_name,
Body=object_content
)
print("Object: {0} uploaded!".format(object_name))
def objectlock_retention(bucket_name,object_name):
# Put objectlock retenion on the object uploaded to the bucket.
date = datetime.now()+timedelta(seconds=5)
retention_rule = {'Mode': 'COMPLIANCE', 'RetainUntilDate': date}
cos_cli.put_object_retention(Bucket=bucket_name, Key=object_name, Retention=retention_rule)
# Get objectlock retention of the above object.
response = cos_cli.get_object_retention(Bucket=bucket_name, Key=object_name)
print("Objectlock Retention for {0}=>".format(object_name))
print(response.Retention)
def objectlock_legal-hold(bucket_name,object_name):
# Setting the objectlock legal-hold status to ON.
cos_cli.put_object_legal_hold(Bucket=bucket_name, Key=object_name, legal-hold={'Status': 'ON'})
# Get objectlock retention of the above object.
response = cos_cli.get_object_legal_hold(Bucket=bucket_name, Key=object_name)
print("Objectlock legal-hold for {0}=>".format(object_name))
print(response.legal-hold)
COS_ENDPOINT = "" #Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints -> Ex:https://s3.us-south.cloud-object-storage.appdomain.cloud
COS_API_KEY_ID = "" #API Key of the cos instance created Ex: W00YixxxxxxxxxxMB-odB-2ySfTrFBIQQWanc--P3byk
COS_RESOURCE_INSTANCE_CRN = "" #API key of cos instance example: xxxd12V2QHXbjaM99G9tWyYDgF_0gYdlQ8aWALIQxXx4
# Create client connection
cos_cli = ibm_boto3.client("s3",
ibm_api_key_id=COS_API_KEY_ID,
config=Config(signature_version="oauth"),
endpoint_url=COS_ENDPOINT,
ibm_service_instance_id=COS_RESOURCE_INSTANCE_CRN,
ibm_auth_endpoint="https://iam.test.cloud.ibm.com/identity/token"
)
new_bucket_name = "create-example-python12345" # bucket name should be unique gloablly, or else it will throw an error.
new_text_file_name = "cos_object.txt"
new_text_file_contents = "This is a test file from Python code sample!!!"
# *** Main Program ***
def main():
create_bucket_with_objectlock(new_bucket_name) # Create a new cos bucket with object lock enabled.
objectlock_configuration_on_bucket(new_bucket_name) # Put objectlock configuration(i.e. default retention) on COS bucket and get the configuration.
upload_object(new_bucket_name,new_text_file_name,new_text_file_contents) # Upload an object to cos bucket.
objectlock_retention(new_bucket_name,new_text_file_name) # Put objectlock retention(i.e. retain until date) on the object and get the configured retention.
objectlock_legal-hold(new_bucket_name,new_text_file_name) # Put objectlock legal-hold on the object and get the legal-hold status.
if __name__ == "__main__":
main()
Node.js
使用 IBM COS SDK for Node.js 啟用版本化:
'use strict';
// Required libraries
const ibm = require('ibm-cos-sdk');
const fs = require('fs');
const crypto = require('crypto');
function logError(e) {
console.log(`ERROR: ${e.code} - ${e.message}\n`);
}
function logDone() {
console.log('DONE!\n');
}
const COS_ENDPOINT = ""; //Choose endpoint from https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints. Ex: https://s3.us-south.cloud-object-storage.appdomain.cloud
const COS_API_KEY_ID = ""; // API key of cos instance example: xxxd12V2QHXbjaM99G9tWyYDgF_0gYdlQ8aWALIQxXx4
const COS_AUTH_ENDPOINT = "";
const COS_RESOURCE_INSTANCE_CRN = ""; // example: crn:v1:bluemix:public:cloud-object-storage:global:a <CREDENTIAL_ID_AS_GENERATED>:<SERVICE_ID_AS_GENERATED>::
// Client Creation.
var config = {
endpoint: COS_ENDPOINT,
apiKeyId: COS_API_KEY_ID,
ibmAuthEndpoint: COS_AUTH_ENDPOINT,
serviceInstanceId: COS_RESOURCE_INSTANCE_CRN,
signatureVersion: 'iam'
};
var cos = new ibm.S3(config);
// Create new bucket with objectlock enabled.
function createBucket(bucketName) {
console.log(`Creating new bucket: ${bucketName}`);
return cos.createBucket({
Bucket: bucketName,
ObjectLockEnabledForBucket: true,
CreateBucketConfiguration: {
LocationConstraint: ''
},
}).promise()
.then((() => {
console.log(`Bucket: ${bucketName} created!`);
}))
.catch((e) => {
console.error(`ERROR: ${e.code} - ${e.message}\n`);
});
}
// Create new text file and upload the object to COS bucket.
function createTextFile(bucketName, itemName, fileText) {
console.log(`Creating new item: ${itemName}`);
return cos.putObject({
Bucket: bucketName,
Key: itemName,
Body: fileText
}).promise()
.then(() => {
console.log(`Item: ${itemName} created!`);
logDone();
})
.catch(logError);
}
function putObjectLockConfigurationonBucket(bucketName) {
console.log(`Putting Objectlock Configuration on : ${bucketName}`);
// Putting objectlock configuration
var defaultRetention = {Mode: 'COMPLIANCE', Days: 1}
var objectLockRule = {DefaultRetention : defaultRetention}
var param = {ObjectLockEnabled: 'Enabled', Rule: objectLockRule}
return cos.putObjectLockConfiguration({
Bucket: bucketName,
ObjectLockConfiguration: param
}).promise()
.then(() => {
console.log(`Object lock Configurtion added!!`);
logDone();
})
.catch(logError);
}
function getObjectLockConfigurationonBucket(bucketName) {
console.log(`Getting Objectlock Configuration for : ${bucketName}`);
// Getting objectlock configuration
return cos.getObjectLockConfiguration({
Bucket: bucketName,
}).promise()
.then((data) => {
console.log(`objectlock configuration`);
console.log( JSON.stringify(data.ObjectLockConfiguration, null, " ") );
logDone();
})
.catch(logError);
}
function putObjectLockRetention(bucketName,keyName) {
console.log(`Putting Objectlock Retention on : ${keyName}`);
var inFiveSecond = (new Date(Date.now() + (1000 * 5)))
var rule = {Mode: 'COMPLIANCE', RetainUntilDate: inFiveSecond}
// Putting objectlock retention
return cos.putObjectRetention({
Bucket: bucketName,
Key: keyName,
Retention: rule
}).promise()
.then(() => {
console.log(`Object lock Retention added!!`);
logDone();
})
.catch(logError);
}
function getObjectLockRetention(bucketName,keyName) {
console.log(`Getting Objectlock Retention for : ${keyName}`);
// Getting objectlock retention
return cos.getObjectRetention({
Bucket: bucketName,
Key: keyName
}).promise()
.then((data) => {
console.log(`Objectlock retention for : ${keyName} `);
console.log( JSON.stringify(data.Retention, null, " ") );
logDone();
})
.catch(logError);
}
function putObjectLocklegal-hold(bucketName,keyName) {
console.log(`Putting Objectlock legal-hold status ON for : ${keyName}`);
// Putting objectlock legal-hold status
return cos.putObjectlegal-hold({
Bucket: bucketName,
Key: keyName,
legal-hold: {Status: 'ON'}
}).promise()
.then(() => {
console.log(`Object lock legal-hold added!!`);
logDone();
})
.catch(logError);
}
function getObjectLocklegal-hold(bucketName,keyName) {
console.log(`Getting Objectlock legal-hold for : ${keyName}`);
// Getting objectlock legal-hold
return cos.getObjectlegal-hold({
Bucket: bucketName,
Key: keyName
}).promise()
.then((data) => {
console.log(`Objectlock legal-hold for : ${keyName} `);
console.log( JSON.stringify(data.legal-hold, null, " ") );
logDone();
})
.catch(logError);
}
// Main app
function main() {
try {
var newBucketName = "jscosbucket350";
var newTextFileName = "js_cos_bucket_file.txt";
var newTextFileContents = "This is a test file from Node.js code sample!!!";
createBucket(newBucketName) // Create a new cos bucket with object lock enabled.
.then(() => putObjectLockConfigurationonBucket(newBucketName)) // Put objectlock configuration(i.e. default retention) on COS bucket.
.then(() => getObjectLockConfigurationonBucket(newBucketName)) // Read objectlock configuration on COS bucket.
.then(() => createTextFile(newBucketName, newTextFileName, newTextFileContents)) // Upload an object to cos bucket.
.then(() => putObjectLockRetention(newBucketName, newTextFileName)) // Put objectlock retention(i.e. retain until date) on the object.
.then(() => getObjectLockRetention(newBucketName, newTextFileName)) // Get the configured retention.
.then(() => putObjectLocklegal-hold(newBucketName,newTextFileName)) // Put objectlock legal-hold on the object.
.then(() => getObjectLocklegal-hold(newBucketName,newTextFileName)); // Get the legal-hold status.
}
catch(ex) {
logError(ex);
}
}
main();
進行
package main
import (
"bytes"
"fmt"
"time"
"github.com/IBM/ibm-cos-sdk-go/aws"
"github.com/IBM/ibm-cos-sdk-go/aws/credentials/ibmiam"
"github.com/IBM/ibm-cos-sdk-go/aws/session"
"github.com/IBM/ibm-cos-sdk-go/service/s3"
)
const (
apiKey = "<apiKey>"
serviceInstanceID = "<serviceInstanceID>"
authEndpoint = "https://iam.cloud.ibm.com/identity/token"
serviceEndpoint = "https://<endpoint>.appdomain.cloud"
)
// Create new bucket with objectlock enabled.
func createBucket(bucketName string, client *s3.S3) {
createBucketInput := new(s3.CreateBucketInput)
createBucketInput.Bucket = aws.String(bucketName)
createBucketInput.ObjectLockEnabledForBucket = aws.Bool(true)
_, e := client.CreateBucket(createBucketInput)
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Bucket Created !!! ")
}
}
func uploadObject(bucketName string, client *s3.S3, fileName string, fileContent string) {
putInput := &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
Body: bytes.NewReader([]byte(fileContent)),
}
_, e := client.PutObject(putInput)
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Object Uploaded!!! ")
}
}
func objectLockConfiguration(bucketName string, client *s3.S3) {
// Putting default retenion on the COS bucket.
putObjectLockConfigurationInput := &s3.PutObjectLockConfigurationInput{
Bucket: aws.String(bucketName),
ObjectLockConfiguration: &s3.ObjectLockConfiguration{
ObjectLockEnabled: aws.String(s3.ObjectLockEnabledEnabled),
Rule: &s3.ObjectLockRule{
DefaultRetention: &s3.DefaultRetention{
Mode: aws.String("COMPLIANCE"),
Days: aws.Int64(1),
},
},
},
}
_, e := client.PutObjectLockConfiguration(putObjectLockConfigurationInput)
// Reading the objectlock configuration set on the bucket.
getObjectLockConfigurationInput := new(s3.GetObjectLockConfigurationInput)
getObjectLockConfigurationInput.Bucket = aws.String(bucketName)
response, e := client.GetObjectLockConfiguration(getObjectLockConfigurationInput)
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Object Lock Configuration =>", response.ObjectLockConfiguration)
}
}
func objectLockRetention(bucketName string, client *s3.S3, keyName string) {
// Put objectlock retenion on the object uploaded to the bucket.
retention_date := time.Now().Local().Add(time.Second * 5)
putObjectRetentionInput := &s3.PutObjectRetentionInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
Retention: &s3.ObjectLockRetention{
Mode: aws.String("COMPLIANCE"),
RetainUntilDate: aws.Time(retention_date),
},
}
_, e := client.PutObjectRetention(putObjectRetentionInput)
// Get objectlock retention of the above object.
getObjectRetentionInput := new(s3.GetObjectRetentionInput)
getObjectRetentionInput.Bucket = aws.String(bucketName)
getObjectRetentionInput.Key = aws.String(keyName)
response, e := client.GetObjectRetention(getObjectRetentionInput)
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Object Lock Retention =>", response.Retention)
}
}
func objectLocklegal-hold(bucketName string, client *s3.S3, keyName string) {
// Setting the objectlock legal-hold status to ON.
putObjectlegal-holdInput := &s3.PutObjectlegal-holdInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
legal-hold: &s3.ObjectLocklegal-hold{
Status: aws.String("ON"),
},
}
_, e := client.PutObjectlegal-hold(putObjectlegal-holdInput)
// Get objectlock retention of the above object.
getObjectlegal-holdInput := new(s3.GetObjectlegal-holdInput)
getObjectlegal-holdInput.Bucket = aws.String(bucketName)
getObjectlegal-holdInput.Key = aws.String(keyName)
response, e := client.GetObjectlegal-hold(getObjectlegal-holdInput)
if e != nil {
fmt.Println(e)
} else {
fmt.Println("Object Lock legal-hold =>", response.legal-hold)
}
}
func main() {
bucketName := "gocosbucket353"
textFileName := "go_cos_bucket_file.txt"
textFileContents := "This is a test file from Node.js code sample!!!"
conf := aws.NewConfig().
WithEndpoint(serviceEndpoint).
WithCredentials(ibmiam.NewStaticCredentials(aws.NewConfig(),
authEndpoint, apiKey, serviceInstanceID)).
WithS3ForcePathStyle(true)
sess := session.Must(session.NewSession())
client := s3.New(sess, conf)
createBucket(bucketName, client) // Create a new cos bucket with object lock enabled.
objectLockConfiguration(bucketName, client) // Put objectlock configuration(i.e. default retention) on COS bucket and get the configuration.
uploadObject(bucketName, client, textFileName, textFileContents) // Upload an object to cos bucket.
objectLockRetention(bucketName, client, textFileName) // Put objectlock retention(i.e. retain until date) on the object and get the configured retention.
objectLocklegal-hold(bucketName, client, textFileName) // Put objectlock legal-hold on the object and get the legal-hold status.
}
Terraform
// Create COS instance.
resource "ibm_resource_instance" "cos_instance" {
name = "cos-instance"
resource_group_id = data.ibm_resource_group.cos_group.id
service = "cloud-object-storage"
plan = "standard"
location = "global"
}
// Create a new bucket with objectlock and object versioning enabled.
resource "ibm_cos_bucket" "bucket" {
bucket_name = var.bucket_name
resource_instance_id = ibm_resource_instance.cos_instance.id
region_location = var.regional_loc
storage_class = var.standard_storage_class
object_versioning {
enable = true
}
object_lock = true
}
// Set object lock configuration on the bucket by providing the crn of the new COS bucket.
resource ibm_cos_bucket_objectlock_configuration "objectlock" {
bucket_crn = ibm_cos_bucket.bucket.crn
bucket_location = var.regional_loc
object_lock_configuration{
objectlockenabled = "Enabled"
objectlockrule{
defaultretention{
mode = "COMPLIANCE"
days = 6
}
}
}
}
// Upload an object to the COS bucket with objectlock retention and objectlock legal-hold.
resource "ibm_cos_bucket_object" "object_object_lock" {
bucket_crn = ibm_cos_bucket.bucket.crn
bucket_location = ibm_cos_bucket.bucket.region_location
content = "Hello World 2"
key = "plaintext5.txt"
object_lock_mode = "COMPLIANCE"
object_lock_retain_until_date = "2023-02-15T18:00:00Z"
object_lock_legal_hold_status = "ON"
force_delete = true
}