按照前置字串篩選

本頁面說明如何使用納入和排除前置字串,在轉移作業中納入及排除路徑。

如要瞭解如何建立要轉移的特定物件資訊清單,請參閱「使用資訊清單轉移特定檔案或物件」一文。

總覽

Storage 移轉服務支援使用前置字串,選擇要從資料來源納入或排除的檔案。您可以使用納入前置字串、排除前置字串,或是同時採用兩者。

您可以依據前置字串篩選 Amazon S3、Microsoft Azure Blob 儲存體和 Cloud Storage 資料來源。

  • 請勿加入前置字串開頭的正斜線。舉例而言,從下列值區路徑 s3://my-aws-bucket/logs/y=2015/requests.gz 移轉時,如果要納入 requests.gz 物件,請將「納入」前置字串指定為 logs/y=2015/requests.gz

  • 系統支援部分相符的納入和排除前置字串。例如,path 符合 path_1/path_2/

  • 不支援萬用字元。

  • 如果您指定資料夾做為來源位置,前置字元篩選器會相對於該資料夾。舉例來說,如果來源是 gs://my-test-bucket/path/,則 file 的包含篩選器會納入所有以 gs://my-test-bucket/path/file 開頭的檔案。

  • 每個 include 前置字元都必須包含物件命名空間的不同部分。任何內含前置字串不得為其他內含前置字串的前置字串。舉例來說,您不得同時將 path_1path_1/subpath_2 指定為包含前置字串。

  • 如果要同時使用「納入」前置字串及「排除」前置字串,「排除」前置字串的開頭則必須為其中一個「納入」前置字串的值。舉例來說,如果指定 a 做為「納入」前置字串,有效的「排除」前置字串即為 a/baaaabc

  • 如果您只要使用「排除」前置字串,則可使用的前置字串並無限制。

  • 如果不指定任何前置字串,則值區中的所有物件都會一併移轉。

如需前置字串的一般資訊,請參閱 Amazon S3 說明文件中的「使用前置字串與分隔符號以階層方式列出金鑰」或 Cloud Storage 的「物件清單列出方法」。

如何指定前置字串

Cloud 控制台

如要使用 Cloud 控制台指定納入和排除前置字串,請在建立新轉移更新現有轉移時輸入值。

gcloud CLI

如要使用 gcloud CLI 指定納入和排除前置字串,請將 --include-prefixes--exclude-prefixes 旗標傳遞至 gcloud transfer jobs create 指令或 gcloud transfer jobs update 指令:

gcloud transfer jobs create SOURCE DESTINATION \
  --include-prefixes="path_1/,path_2/" --exclude-prefixes="path_1/subpath_2/"

請使用半形逗號分隔多個前置字串,並省略半形逗號後面的空格。例如 --include-prefixes=foo,bar

REST

如要使用 REST API 指定納入和排除前置字串,請使用 includePrefixes[]excludePrefixes[] 欄位:

{
    "description": "YOUR DESCRIPTION",
    "status": "ENABLED",
    "projectId": "PROJECT_ID",
    "schedule": {
        "scheduleStartDate": {
            "day": 1,
            "month": 1,
            "year": 2015
        },
        "startTimeOfDay": {
            "hours": 1,
            "minutes": 1
        }
    },
    "transferSpec": {
        "gcsDataSource": {
            "bucketName": "GCS_SOURCE_NAME"
        },
        "gcsDataSink": {
            "bucketName": "GCS_SINK_NAME"
        },
        "transferOptions": {
            "deleteObjectsFromSourceAfterTransfer": true
        },
        "objectConditions": {
            "includePrefixes": [
                "path_1/",
                "path_2/"
            ],
            "excludePrefixes": [
                "path_1/subpath_2/object_5"
            ]
        }
    }
}

詳情請參閱 ObjectConditions 參考資料。

物件和路徑範例

本文件中的範例使用下列範例物件和路徑:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

加入前置字串

建立移轉作業時,請使用 includePrefix 指示 Storage 移轉服務考慮列出路徑中的物件,並忽略不在這些路徑下的物件。

舉例來說,如要納入 path_1/ 下的物件,請使用下列前置字串:

path_1/

這包括直接位於 path_1/path_1/subpath_1path_1/subpath_2/ 底下的物件。轉移作業會包含下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

您可以指定要納入的多個路徑。例如,您可以傳遞下列內容:

path_1/subpath_2/
path_1/subpath_3/

在這種情況下,轉移作業會包含下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

系統支援部分比對。舉例來說,如果將 path 指定為納入前置字串的值,就會比對下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

使用納入前置字串時,未特別納入的路徑不會轉移至 Cloud Storage 目的地值區。

排除前置字串

在建立轉移作業時使用排除前置字串,可指示 Storage 移轉服務忽略所列的轉移路徑。

如要排除 path_1/ 下的物件,請傳遞下列前置字串:

path_1/

這會排除 path_1/path_1/subpath_1/path_1/subpath_2/ 下的物件。在這種情況下,轉移作業會納入下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

您可以指定多個要排除的路徑。例如,您可以傳遞下列內容:

path_1/subpath_2/
path_2/subpath_3/

在這種情況下,轉移作業會包含下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

同時納入及排除路徑

您可以同時套用排除前置字串和納入前置字串,在這種情況下,排除前置字串會限制納入前置字串在移轉作業中納入的內容。

同時指定兩種前置字串時,每個排除前置字串的開頭都必須是納入前置字串中指定的路徑。

舉例來說,如要納入 path_1/ 下的物件,並排除 subpath_1/ 下的物件,請傳遞以下內容:

include: path_1/
exclude: path_1/subpath_1/

在這種情況下,轉移作業會包含下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

如要納入 path_1/path_2/ 下方的所有物件 (path_1/subpath_1/path_2/subpath_3/ 中的項目除外),請傳遞以下內容:

include: path_1/
         path_2/
exclude: path_1/subpath_1/
         path_2/subpath_3/

在這種情況下,轉移作業會包含下列物件:

xx://bucketname/object_1
xx://bucketname/object_2
xx://bucketname/path_1/object_3
xx://bucketname/path_2/object_4
xx://bucketname/path_1/subpath_1/object_5
xx://bucketname/path_1/subpath_2/object_6
xx://bucketname/path_2/subpath_3/object_7
xx://bucketname/path_2/subpath_4/object_8

錯誤的路徑納入或排除示例

以下各節提供使用納入或排除路徑時應避免的範例,以及如何修正這些路徑,讓它們正常運作。

納入其他納入前置字串中使用的路徑

每個 include 前置字元都應指定物件命名空間的不同部分。以下範例不正確,因為第二個值已包含在第一個值的命名空間中:

include: path_1/
         path_1/subpath_1

在本例中,由於第二個 include 前置字元 path_1/subpath_1 已包含在 path_1/ 中,因此 include 前置字元值無效。如要修正這個問題,請移除其中一個值。

使用開頭不是納入前置字串的排除前置字串

每個排除前置字串的開頭都必須是任何指定的納入前置字串值。以下範例不正確,因為排除前置字串值的開頭並非指定的納入前置字串值:

include: path_1/
         path_2/
exclude: subpath_1
         subpath_4

在本例中,「排除」前置字串的值無效,因為這些值的開頭並非任一「納入」前置字串的值。如要修正這個問題,請確認排除前置字串包含列為納入前置字串的完整路徑:

include: path_1/
         path_2/
exclude: path_1/subpath_1/
         path_2/subpath_4/

後續步驟