更新时间:2024-07-17 GMT+08:00
分享

获取指定目录文件列表

功能介绍

在MRS集群中获取指定目录文件列表。

接口约束

调用方法

请参见如何调用API

URI

GET /v2/{project_id}/clusters/{cluster_id}/files

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释:

项目编号。获取方法,请参见获取项目ID

约束限制:

不涉及

取值范围:

只能由英文字母和数字组成,且长度为[1-64]个字符。

默认取值:

不涉及

cluster_id

String

参数解释:

集群ID。如果指定集群ID,则获取该集群做过补丁更新的最新版本元数据。获取方法,请参见获取集群ID

约束限制:

不涉及

取值范围:

只能由英文字母、数字以及“_”和“-”组成,且长度为[1-64]个字符。

默认取值:

不涉及

表2 Query参数

参数

是否必选

参数类型

描述

path

String

参数解释:

文件目录。比如访问“/tmp/test”目录列表,此处必须是目录,整体URI为/v2/{project_id}/clusters/{cluster_id}/files?path=%2Ftmp%2Ftest。

约束限制:

不涉及

取值范围:

单层目录要遵循以下规则:

  • 不能为空

  • 不能以"."开头或结尾

  • 不能包括下列符号 : :*?"<>|;&,'`!{}[]$%+

  • 不能超过255个字节

默认取值:

不涉及

offset

String

参数解释:

分页参数,表示从该偏移量开始查询文件列表。

约束限制:

不涉及

取值范围:

不涉及

默认取值:

1

limit

String

参数解释:

分页参数,列表当前分页的数量限制。

约束限制:

不涉及

取值范围:

≤1000

默认取值:

100

sort_key

String

参数解释:

列表排序按该属性排序。

约束限制:

不涉及

取值范围:

  • path_suffix:文件或目录名称

  • length:文件大小

  • modification_time:修改时间

默认取值:

path_suffix

order

String

参数解释:

列表排序方式。

约束限制:

不涉及

取值范围:

  • desc:降序排列

  • asc:升序排列

默认取值:

desc

请求参数

响应参数

状态码: 200

表3 响应Body参数

参数

参数类型

描述

total_count

Long

参数解释:

文件总数,与分页无关。

取值范围:

不涉及

files

Array of FileStatusV2 objects

参数解释:

文件列表。

表4 FileStatusV2

参数

参数类型

描述

path_suffix

String

参数解释:

文件在当前目录下的后缀,如获取“/tmp”目录,下面的“/tmp/test”文件,此处path_suffix内容为“test”。

取值范围:

不涉及

owner

String

参数解释:

文件拥有者。

取值范围:

不涉及

group

String

参数解释:

文件属组。

取值范围:

不涉及

permission

String

参数解释:

权限信息。

取值范围:

不涉及

replication

Integer

参数解释:

副本数。

取值范围:

不涉及

block_size

Integer

参数解释:

块大小。

取值范围:

不涉及

length

Integer

参数解释:

文件长度。

取值范围:

不涉及

type

String

参数解释:

文件类型。

取值范围:

  • FILE:文件

  • DIRECTORY:目录

children_num

Integer

参数解释:

该目录下的文件条目数。

取值范围:

不涉及

access_time

Long

参数解释:

文件访问时间。

取值范围:

不涉及

modification_time

Long

参数解释:

文件修改时间。

取值范围:

不涉及

请求示例

获取指定目录文件列表请求示例

GET /v2/{project_id}/clusters/{cluster_id}/files?path={directory}&offset={offset}&limit={limit}&sort_key={sort_key}&order={order}

响应示例

状态码: 200

获取指定目录文件列表成功

{
  "total_count" : 2,
  "files" : [ {
    "access_time" : 0,
    "block_size" : 0,
    "children_num" : 0,
    "group" : "hadoop",
    "length" : 0,
    "modification_time" : 1587179516623,
    "owner" : "hdfs",
    "path_suffix" : "app-logs",
    "permission" : 777,
    "replication" : 0,
    "type" : "DIRECTORY"
  }, {
    "access_time" : 1587267212761,
    "block_size" : 134217728,
    "children_num" : 0,
    "group" : "hadoop",
    "length" : 23666188,
    "modification_time" : 1587222156003,
    "owner" : "root",
    "path_suffix" : "data-m-00000",
    "permission" : "644",
    "replication" : 3,
    "type" : "FILE"
  } ]
}

SDK代码示例

SDK代码示例如下。

 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
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.mrs.v2.region.MrsRegion;
import com.huaweicloud.sdk.mrs.v2.*;
import com.huaweicloud.sdk.mrs.v2.model.*;


public class ShowHdfsFileListSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        MrsClient client = MrsClient.newBuilder()
                .withCredential(auth)
                .withRegion(MrsRegion.valueOf("<YOUR REGION>"))
                .build();
        ShowHdfsFileListRequest request = new ShowHdfsFileListRequest();
        request.withPath("<path>");
        request.withOffset("<offset>");
        request.withLimit("<limit>");
        request.withSortKey(ShowHdfsFileListRequest.SortKeyEnum.fromValue("<sort_key>"));
        request.withOrder(ShowHdfsFileListRequest.OrderEnum.fromValue("<order>"));
        try {
            ShowHdfsFileListResponse response = client.showHdfsFileList(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
 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
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkmrs.v2.region.mrs_region import MrsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkmrs.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]

    credentials = BasicCredentials(ak, sk)

    client = MrsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(MrsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ShowHdfsFileListRequest()
        request.path = "<path>"
        request.offset = "<offset>"
        request.limit = "<limit>"
        request.sort_key = "<sort_key>"
        request.order = "<order>"
        response = client.show_hdfs_file_list(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
 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
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    mrs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mrs/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := mrs.NewMrsClient(
        mrs.MrsClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ShowHdfsFileListRequest{}
	request.Path = "<path>"
	offsetRequest:= "<offset>"
	request.Offset = &offsetRequest
	limitRequest:= "<limit>"
	request.Limit = &limitRequest
	sortKeyRequest:= model.GetShowHdfsFileListRequestSortKeyEnum().<SORT_KEY>
	request.SortKey = &sortKeyRequest
	orderRequest:= model.GetShowHdfsFileListRequestOrderEnum().<ORDER>
	request.Order = &orderRequest
	response, err := client.ShowHdfsFileList(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

获取指定目录文件列表成功

错误码

请参见错误码

相关文档