如果您希望篩選OSS Bucket中指定時間範圍的Object,您可以使用資料索引功能。通過設定Object最後修改時間的起始和結束日期,提高查詢效率,適用於審計追蹤、資料同步、周期性備份、成本分析或其他業務情境中對時效性檔案的精準擷取需求。
使用情境
審計與合規性檢查
企業或組織需定期追溯資料活動以確保合規。查詢指定時間範圍內的上傳檔案有助於資料生命週期管理、追蹤資料產生與變更歷史,以及資料審計過程。
資料備份與恢複
在指定備份策略下進行備份或恢複操作時,查詢指定時間範圍內的新增或更新檔案有助於明確備份對象,節省儲存空間和傳輸成本。
資料分析與報表產生
對於涉及巨量資料分析、Tlog或業務報表的應用,需要定期(如每日、每周、每月)處理某一時間段內產生的新資料。查詢特定時間範圍內的檔案可以快速定位到待分析的資料集,簡化資料提取流程。
內容管理系統同步
內容管理系統需定期同步新增或更新的檔案,以保持網站內容的時效性和完整性。
成本最佳化與資源清理
企業需定期清理不再需要的舊檔案。查詢指定時間內未更新的檔案有助於識別不再需要保留的檔案,確保資源有效利用
故障排查與問題追溯
在排查系統故障、資料丟失或異常行為時,查詢特定時間段內的檔案有助於快速定位問題檔案或操作記錄。
協作與版本控制
在多使用者協作環境中,查詢時間範圍內的檔案有助於歷史版本瀏覽、對比或恢複到特定時間點的狀態。
操作步驟
使用OSS控制台
登入OSS管理主控台。
單擊Bucket 列表,然後單擊目標Bucket名稱。
在左側導覽列, 選擇文件管理 > 數據索引。
在數據索引頁面,開啟中繼資料管理開關。
開啟中繼資料管理需要等待一段時間,具體時間長度取決於Bucket中Object的數量。
在oss中繼資料檢索條件地區,設定最後修改時間的起始和結束日期(精確到秒),其他參數保留預設配置。
在對象排序方式地區,選擇根據檔案名稱升序排列。
單擊查詢。
使用阿里雲SDK
僅Java SDK、Python SDK以及Go SDK支援通過資料索引功能查詢滿足指定條件的Object。使用資料索引功能前,您需要為指定Bucket開啟中繼資料管理功能。
Java
package com.aliyun.sts.sample;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
public class Demo {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
private static String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 填寫Endpoint對應的Region資訊,例如cn-hangzhou。
private static String region = "cn-hangzhou";
// 填寫Bucket名稱,例如examplebucket。
private static String bucketName = "examplebucket";
public static void main(String[] args) throws Exception {
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 建立OSSClient執行個體。
// 當OSSClient執行個體不再使用時,調用shutdown方法以釋放資源。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// 顯式聲明使用 V4 簽名演算法
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 設定返回的檔案最大個數為100。
int maxResults = 100;
// 指定查詢最後更新時間在2023年12月01日至2024年03月31日範圍內的檔案。
String query = "{\n" +
" \"SubQueries\":[\n" +
" {\n" +
" \"Field\":\"FileModifiedTime\",\n" +
" \"Value\": \"2023-12-01T00:00:00.000+08:00\",\n" +
" \"Operation\":\"gt\"\n" +
" }, \n" +
" {\n" +
" \"Field\":\"FileModifiedTime\",\n" +
" \"Value\": \"2024-03-31T23:59:59.000+08:00\",\n" +
" \"Operation\":\"lt\"\n" +
" }\n" +
" ],\n" +
" \"Operation\":\"and\"\n" +
"}";
// 指定返回結果按檔案名稱升序排列。
String sort = "Filename";
DoMetaQueryRequest doMetaQueryRequest = new DoMetaQueryRequest(bucketName, maxResults, query, sort);
doMetaQueryRequest.setOrder(SortOrder.ASC);
DoMetaQueryResult doMetaQueryResult = ossClient.doMetaQuery(doMetaQueryRequest);
if (doMetaQueryResult.getFiles() != null) {
for (ObjectFile file : doMetaQueryResult.getFiles().getFile()) {
System.out.println("Filename: " + file.getFilename());
// 擷取標識Object的內容。
System.out.println("ETag: " + file.getETag());
// 擷取Object的存取權限
System.out.println("ObjectACL: " + file.getObjectACL());
// 擷取Object的類型。
System.out.println("OssObjectType: " + file.getOssObjectType());
// 擷取Object的儲存類型。
System.out.println("OssStorageClass: " + file.getOssStorageClass());
// 擷取Object的標籤個數。
System.out.println("TaggingCount: " + file.getOssTaggingCount());
if (file.getOssTagging() != null) {
for (Tagging tag : file.getOssTagging().getTagging()) {
System.out.println("Key: " + tag.getKey());
System.out.println("Value: " + tag.getValue());
}
}
if (file.getOssUserMeta() != null) {
for (UserMeta meta : file.getOssUserMeta().getUserMeta()) {
System.out.println("Key: " + meta.getKey());
System.out.println("Value: " + meta.getValue());
}
}
}
}
} catch (OSSException oe) {
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Error Message: " + ce.getMessage());
} finally {
// 關閉OSSClient。
ossClient.shutdown();
}
}
}Python
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import MetaQuery, AggregationsRequest
# 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Endpoint以杭州為例,其它Region請按實際情況填寫。
# 填寫Bucket名稱,例如examplebucket。
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'examplebucket0703')
# 指定查詢最後更新時間在2023年12月01日至2024年03月31日範圍內的檔案,返回結果按檔案名稱升序排列。
do_meta_query_request = MetaQuery(max_results=100, query='{"SubQueries":[{"Field":"FileModifiedTime","Value": "2023-12-01T00:00:00.000+08:00","Operation":"gt"}, {"Field":"FileModifiedTime","Value": "2024-03-31T23:59:59.000+08:00","Operation":"lt"}],"Operation":"and"}', sort='Filename', order='asc')
result = bucket.do_bucket_meta_query(do_meta_query_request)
for s in result.files:
print(s.file_name)
print(s.etag)
print(s.oss_object_type)
print(s.oss_storage_class)
print(s.oss_crc64)
print(s.object_acl)Go
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 建立OSSClient執行個體。
// yourEndpoint填寫Bucket對應的Endpoint,以華東1(杭州)為例,填寫為https://oss-cn-hangzhou.aliyuncs.com。其它Region請按實際情況填寫。
client, err := oss.New("oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 指定查詢最後更新時間在2023年12月01日至2024年03月31日範圍內的檔案。
query := oss.MetaQuery{
NextToken: "",
MaxResults: 100,
Query: `{
"SubQueries":[
{
"Field":"FileModifiedTime",
"Value": "2023-12-01T00:00:00.000+08:00",
"Operation":"gt"
},
{
"Field":"FileModifiedTime",
"Value": "2024-03-31T23:59:59.000+08:00",
"Operation":"lt"
}
],
"Operation":"and"
}`,
// 指定返回結果按檔案名稱升序排列。
Sort: "Filename",
Order: "asc",
}
// 查詢滿足指定條件的Object,並按照指定欄位和排序方式列舉Object資訊。
result, err := client.DoMetaQuery("examplebucket", query)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("NextToken:%s\n", result.NextToken)
for _, file := range result.Files {
fmt.Printf("File name: %s\n", file.Filename)
fmt.Printf("size: %d\n", file.Size)
fmt.Printf("File Modified Time:%s\n", file.FileModifiedTime)
fmt.Printf("Oss Object Type:%s\n", file.OssObjectType)
fmt.Printf("Oss Storage Class:%s\n", file.OssStorageClass)
fmt.Printf("Object ACL:%s\n", file.ObjectACL)
fmt.Printf("ETag:%s\n", file.ETag)
fmt.Printf("Oss CRC64:%s\n", file.OssCRC64)
fmt.Printf("Oss Tagging Count:%d\n", file.OssTaggingCount)
for _, tagging := range file.OssTagging {
fmt.Printf("Oss Tagging Key:%s\n", tagging.Key)
fmt.Printf("Oss Tagging Value:%s\n", tagging.Value)
}
for _, userMeta := range file.OssUserMeta {
fmt.Printf("Oss User Meta Key:%s\n", userMeta.Key)
fmt.Printf("Oss User Meta Key Value:%s\n", userMeta.Value)
}
}
}
使用REST API
如果您的程式自訂要求較高,您可以直接發起REST API請求。直接發起REST API請求需要手動編寫代碼計算簽名。更多資訊,請參見DoMetaQuery。
相關文檔
資料索引支援儲存類型、讀寫權限、檔案大小等多項過濾條件,協助您快速篩選符合要求的資料。例如,您可以查詢所有讀寫權限為公用讀取的檔案,或所有小於64 KB的檔案。更多資訊,請參見標量檢索。