b2_list_file_names
    • Dark
      Light

    b2_list_file_names

    • Dark
      Light

    Article Summary

    Get
    /b2api/v3/b2_list_file_names

    PLEASE NOTE:

    This API request is now being described as a GET request and not a POST request, as was the case in previous versions of our documentation.

    We made this update because we believe that an API call that retrieves data and does not alter state is more accurately represented as a GET request.

    You may still make a POST request to this endpoint by simply submitting a JSON-formatted request body containing the parameters, with Content-Type set to application/json.


    Lists the names of all files in a bucket, starting at a given name

    This call returns at most 10,000 file names per transaction, but it can be called repeatedly to scan through all of the file names in a bucket. Each time you call, it returns a "nextFileName" that can be used as the starting point for the next call.

    Please note: billing is per 1000 files/versions returned. For example, if 10,000 file names are requested and returned, you will be billed for 10 API calls.

    There may be many file versions for the same name, but this call will return each name only once. Files that have been hidden will not be returned; to see all versions of a file, including hide markers, use b2_list_file_versions instead.

    To go through all of the file names in a bucket, use a loop like this:

    startFileName = null   
    while true:
        response = b2_list_file_names(bucketId = ..., startFileName = startFileName)
        for file in response.files:
            process_one_file_name(file)
        if response.startFileName == null:
            break
        startFileName = response.nextFileName
    

    Prefixes and Delimiters

    You can optionally specify a file name prefix, which will restrict the results to only files starting with that prefix.

    Another option is to specify a delimiter (usually "/") for folder names. If found after the file name prefix, the delimiter is treated as the end of a folder name, and the folder name is returned, replacing all of the files in that folder. Each item returned is either an "upload" (a file) or a "folder" (representing one or many files).

    As an example, if you have these files in your bucket:


    photos/alice.jpg
    photos/alfred.jpg
    photos/cats/fluffy.jpg
    photos/cats/mittens.jpg
    photos/dogs/spot.jpg
    photos/lilly.jpg

    With the default prefix of "" and no delimiter, you get all of the files:


    upload photos/alice.jpg
    upload photos/alfred.jpg
    upload photos/cats/fluffy.jpg
    upload photos/cats/mittens.jpg
    upload photos/dogs/spot.jpg
    upload photos/lilly.jpg

    If you use a prefix of "photos/c" you will get the same list as if you specify "photos/cats/" as the prefix:


    upload photos/cats/fluffy.jpg
    upload photos/cats/mittens.jpg

    With a prefix of "" (meaning all files), and a delimiter of "/", you would get:


    folder photos/

    With a prefix of "photos/", and a delimiter of "/", you would get:


    upload photos/alice.jpg
    upload photos/alfred.jpg
    folder photos/cats/
    folder photos/dogs/
    upload photos/lilly.jpg

    Prefixes are parsed as literal strings, so regular expressions and wildcards cannot be used in this context. For example, a prefix of .*cats would only find a match if a file's full name starts with the literal string .*cats.

    Bucket and Prefix Restrictions

    When using an application key that is restricted to one bucket, and to files matching a name prefix in that bucket, you must only ask about those files. Listing files in other buckets is not allowed. Listing files that do not match the prefix is not allowed, so you must specify a prefix that is at least as restrictive as the one in the application key.

    API Versions

    v2: Object Lock (May 11, 2021)

    Now returns fileRetention and legalHold.

    v2: Server-Side Encryption (March 5, 2021)

    Now returns serverSideEncryption.

    v2: Remove application key workaround (Sept 13, 2018)

    Listing file names will always return all of the file names you ask for. If your application key has a file name prefix restriction, and you ask for files outside that prefix, the call is unauthorized.

    Now returns accountId and bucketId. Does not return size any more; use contentLength instead.

    v1: Workaround for existing applications and application keys (August 9, 2018)

    When using an application key with a file name prefix restriction, a request to list files will be filtered to show only files allowed by the application key.

    v1: Original release (September 22, 2015)

    Original release.

    Header parameters
    Authorization
    stringRequired

    An account authorization token, obtained from b2_authorize_account.
    The token must have the listFiles capability.

    Query parameters
    bucketId
    stringRequired

    The bucket to look for file names in. Returned by b2_list_buckets.

    startFileName
    string

    The first file name to return. If there is a file with this name, it will be returned in the list. If not, the first file name after this the first one after this name.

    maxFileCount
    integer

    The maximum number of files to return from this call. The default value is 100, and the maximum is 10000. Passing in 0 means to use the default of 100.


    NOTE: b2_list_file_names is a Class C transaction (see Pricing). The maximum number of files returned per transaction is 1000. If you set maxFileCount to more than 1000 and more than 1000 are returned, the call will be billed as multiple transactions, as if you had made requests in a loop asking for 1000 at a time. For example: if you set maxFileCount to 10000 and 3123 items are returned, you will be billed for 4 Class C transactions.

    prefix
    string

    Files returned will be limited to those with the given prefix. Defaults to the empty string, which matches all files.

    delimiter
    string

    Files returned will be limited to those within the top folder, or any one subfolder. Defaults to NULL. Folder names will also be returned. The delimiter character will be used to "break" file names into folders.

    Responses
    200

    The request succeeded.


    The response headers include the Content-Type that was specified when the file was uploaded. They also include the X-Bz-FileName and X-Bz-Content-Sha1 headers, plus X-Bz-Info-* headers for any custom file info that was provided with the upload. The X-Bz-FileName uses percent-encoding, as if it were a URL parameter.

    Expand All
    object
    files
    Array of object

    An array of objects, each one describing one file or folder. (See below.)

    Example[ { "accountId": "ACCOUNT_ID", "action": "upload", "bucketId": "b2f6f21365e1d29f6c580f18", "contentLength": 7, "contentSha1": "dc724af18fbdd4e59189f5fe768a5f8311527050", "contentType": "text/plain", "fileId": "4_zb2f6f21365e1d29f6c580f18_f10904e5ca06493a1_d20180914_m223119_c002_v0001094_t0002", "fileInfo": { "src_last_modified_millis": "1536964184056" }, "fileName": "testing.txt", "fileRetention": { "isClientAuthorizedToRead": true, "value": { "mode": null, "retainUntilTimestamp": null } }, "legalHold": { "isClientAuthorizedToRead": true, "value": null }, "serverSideEncryption": { "algorithm": "AES256", "mode": "SSE-C" }, "uploadTimestamp": "1536964279000" }, { "accountId": "ACCOUNT_ID", "action": "upload", "bucketId": "b2f6f21365e1d29f6c580f18", "contentLength": 8, "contentSha1": "596b29ec9afea9e461a20610d150939b9c399d93", "contentType": "text/plain", "fileId": "4_zb2f6f21365e1d29f6c580f18_f10076875fe98d4af_d20180914_m223128_c002_v0001108_t0050", "fileInfo": { "src_last_modified_millis": "1536964200750" }, "fileName": "testing2.txt", "fileRetention": { "isClientAuthorizedToRead": true, "value": { "mode": null, "retainUntilTimestamp": null } }, "legalHold": { "isClientAuthorizedToRead": true, "value": null }, "serverSideEncryption": { "algorithm": "AES256", "mode": "SSE-B2" }, "uploadTimestamp": "1536964288000" } ]
    object
    accountId
    string

    The account that owns the file.

    ExampleACCOUNT_ID
    action
    string

    One of "start", "upload", "hide", "folder", or other values added in the future. "upload" means a file that was uploaded to B2 Cloud Storage. "start" means that a large file has been started, but not finished or canceled. "hide" means a file version marking the file as hidden, so that it will not show up in b2_list_file_names. "folder" is used to indicate a virtual folder when listing files.

    Exampleupload
    bucketId
    string

    The bucket that the file is in.

    Exampleb2f6f21365e1d29f6c580f18
    contentLength
    integer

    The number of bytes stored in the file. Only useful when the action is "upload".


    Always 0 when the action is "start", "hide", or "folder".

    Example7
    contentSha1
    string

    The SHA1 of the bytes stored in the file as a 40-digit hex string. Large files do not have SHA1 checksums, and the value is "none".


    The value is null when the action is "hide" or "folder".

    Exampledc724af18fbdd4e59189f5fe768a5f8311527050
    contentMd5
    string

    The MD5 of the bytes stored in the file as a 32-digit hex string. Not all files have an MD5 checksum, so this field is optional, and set to null for files that do not have one. Large files do not have MD5 checksums, and the value is null.


    The value is also null when the action is "hide" or "folder".

    Exampledc724af18fbdd4e59189f5fe768a5f8311527050
    contentType
    string

    When the action is "upload" or "start", the MIME type of the file, as specified when the file was uploaded. For "hide" action, always "application/x-bz-hide-marker". For "folder" action, always null.

    Exampletext/plain
    fileId
    string

    The unique identifier for this version of this file. Used with b2_get_file_info, b2_download_file_by_id, and b2_delete_file_version.


    The value is null when for action "folder".

    Example4_zb2f6f21365e1d29f6c580f18_f10904e5ca06493a1_d20180914_m223119_c002_v0001094_t0002
    fileInfo
    object

    The custom information that was uploaded with the file. This is a JSON object, holding the name/value pairs that were uploaded with the file.

    Example{ "src_last_modified_millis": "1536964200750" }
    fileName
    string

    The name of this file, which can be used with b2_download_file_by_name.

    Exampletesting.txt
    fileRetention
    object

    The Object Lock retention settings for this file, if any. This field is filtered based on application key capabilities; the readFileRetentions capability is required to access the value. See Object Lock for more details on response structure.


    This field is omitted when the action is "hide" or "folder".

    Example{ "isClientAuthorizedToRead": true, "value": { "mode": null, "retainUntilTimestamp": null } }
    isClientAuthorizedToRead
    boolean
    value
    object
    mode
    string
    retainUntilTimestamp
    integer
    legalHold
    object

    The Object Lock legal hold status for this file, if any. This field is filtered based on application key capabilities; the readFileLegalHolds capability is required to access the value. See Object Lock for more details on response structure.


    This field is omitted when the action is "hide" or "folder".

    Example{ "isClientAuthorizedToRead": true, "value": null }
    isClientAuthorizedToRead
    boolean
    value
    string
    replicationStatus
    string

    The Replication Status for this file, if any. This will show either PENDING, COMPLETED, FAILED, or REPLICA For details see Cloud Replication


    This field is omitted when the file is not part of a replication rule.

    serverSideEncryption
    object

    When the file is encrypted with Server-Side Encryption, the mode ("SSE-B2" or "SSE-C") and algorithm used to encrypt the data. If the file is not encrypted with Server-Side Encryption, then both mode and algorithm will be null.


    This field is omitted when the action is "hide" or "folder".

    Example
    algorithm
    string

    Encryption algorithm type.

    ExampleAES256
    mode
    string

    The type of server-side encryption used.

    ExampleSSE-C
    uploadTimestamp
    integer

    This is a UTC time when this file was uploaded. It is a base 10 number of milliseconds since midnight, January 1, 1970 UTC. This fits in a 64 bit integer such as the type "long" in the programming language Java. It is intended to be compatible with Java's time long. For example, it can be passed directly into the java call Date.setTime(long time).


    Always 0 when the action is "folder".

    Example1536964279000
    nextFileName
    string

    What to pass in to startFileName for the next search to continue where this one left off, or null if there are no more files. Note this this may not be the name of an actual file, but using it is guaranteed to find the next file in the bucket.

    ExamplemyNextFile
    400
    statuscodedescription
    400bad_bucket_idThe requested bucket ID does not match an existing bucket.
    400bad_requestThe request had the wrong fields or illegal values. The message returned with the error will describe the problem.
    400invalid_bucket_idInvalid bucketId: <bucketId>
    400out_of_rangemaxFileCount out of range: <maxFileCount>
    object
    status
    integer

    The numeric HTTP status code. Always matches the status in the HTTP response.

    Example400
    code
    string

    A single-identifier code that identifies the error.

    Exampleinvalid_bucket_name
    message
    string

    A human-readable message, in English, saying what went wrong.

    Examplebucket name is too long
    401
    statuscodedescription
    401bad_auth_tokenThe auth token used is not valid. Call b2_authorize_account again to either get a new one, or an error message describing the problem.
    401expired_auth_tokenThe auth token used has expired. Call b2_authorize_account again to get a new one.
    401unauthorizedThe auth token used is valid, but does not authorize this call with these parameters. The capabilities of an auth token are determined by the application key used with b2_authorize_account.
    403
    statuscodedescription
    403transaction_cap_exceededTransaction cap exceeded. To increase your cap, sign in to your B2 Cloud Storage account online. Then select the Caps & Alerts link in the B2 Cloud Storage section of the sidebar.
    429
    statuscodedescription
    429too_many_requestsB2 may limit API requests on a per-account basis.
    503
    statuscodedescription
    503bad_requestTimed out while iterating and skipping files

    Was this article helpful?