curve-client-python-api_en.md 8.9 KB
Newer Older
S
sean 已提交
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
[中文版](../cn/curve-client-python-api.md)

#### Get a CBDClient object that interacts with the Curve cluster

```python
import curvefs

# Get a CBDClient object that interacts with the back-end cluster. Different CBDClient objects can establish connections with different clusters
cbd1 = curvefs.CBDClient()
cbd2 = curvefs.CBDClient()
```

#### CBDClient initialization

```python
import curvefs
cbd = curvefs.CBDClient()
  
# parameter: full path of curve-client configuration file
# return: return 0 if initialized successfully, -1 means failed
cbd.Init("/etc/curve/client.conf")
```

#### Create file

```python
import curvefs
cbd = curvefs.CBDClient()
cbd.Init("/etc/curve/client.conf")  # subsequent examples omit the initialization process
 
# parameter: the three parameters are
#      file full path
#      user information
#      file size
# return: return 0 if create successfully, otherwise return an error code

# First initialize user information (the control plane interfaces of curvefs have user information verification, and user information needs to be passed in)
user = curvefs.UserInfo_t()
user.owner = "curve"
user.password = ""  # when the password is empty, it can be omitted

# Create a file with Create interface
cbd.Create("/curve", user, 10*1024*1024*1024)
  
# UserInfo definition
typedef struct UserInfo {
    char owner[256];       # username 
    char password[256];    # password
} UserInfo_t;
```

#### Get file information

```python
# parameter: the three parameters are
#      filename
#      user information
#      fileInfo[exit parameter]
# return: return 0 if get successfully, otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Construct file information
finfo = curvefs.FileInfo_t()

# Get file information
cbd.StatFile("/curve", user, finfo)
print finfo.filetype
print finfo.length
print finfo.ctime
  
# FileInfo definition
typedef struct FileInfo {
    uint64_t      id;          
    uint64_t      parentid;
    int           filetype;   # volume type
    uint64_t      length;     # volume size
    uint64_t      ctime;      # volume create time
    char          filename[256];   # volume name
    char          owner[256];      # volume owner
    int           fileStatus;      # volume status
} FileInfo_t;
 
# File status
#define CURVE_FILE_CREATED            0
#define CURVE_FILE_DELETING           1
#define CURVE_FILE_CLONING            2
#define CURVE_FILE_CLONEMETAINSTALLED 3
#define CURVE_FILE_CLONED             4
#define CURVE_FILE_BEINGCLONED        5
```

#### Extend file

```python
# parameter: the three parameters are
#      filename
#      user information
#      file size after extend
# return: return 0 if extend successfully, otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Extend
cbd.Extend("/curve", user, 20*1024*1024*1024)

# Get file information after extension
finfo = curvefs.FileInfo_t()
cbd.StatFile("/curve", user, finfo)
print finfo.length
```

#### Open/Close file

```python
# Open file
# parameter: the two parameters are
#      filename
#      user information
# return: return fd if open successfully, otherwise return error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "user1"

# Open file,return fd
fd = cbd.Open("/tmp1", user)
  
# Close file
# parameter: fd returned when opening the file
# return: return 0 if closed successfully, otherwise return an error code
cbd.Close(fd)
```

#### Read and write file

```python
# Write file
# parameter: the four parameters are
#      fd
#      data to write
#      offset
#      date length
# return: return the number of bytes written If write successful, otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "user1"

# Open file,return fd
fd = cbd.Open("/tmp1", user)

# Write files (Currently, 4k alignment is required for reading and writing)
cbd.Write(fd, "aaaaaaaa"*512, 0, 4096)
cbd.Write(fd, "bbbbbbbb"*512, 4096, 4096)
  
# Read file
# parameter: the four parameters are
#      fd
#      null string
#      offset
#      data length to read
# return: return read data if read successfully, otherwise return an error code

# The read content is returned by the return value, buf is not needed, you can pass in an empty string
cbd.Read(fd"", 0, 4096)

# Close file
cbd.Close(fd)
```

Note: The current python api does not support asynchronous reading and writing

#### Delete file

```python
# parameter: the two parameters are
#      filename
#      user information
# return: return 0 if delete successfully, otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Delete file
cbd.Unlink("/curve", user)
```
#### Recover file

```python
# parameter: the three parameters are
#      filename
#      user information
#      file id (optional, default is 0)
# return: return 0 if recover successfully, otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Recover file
cbd.Recover("/curve", user, 0)
```

#### Rename file

```python
# parameter: the three parameters are
#      user information
#      old filename
#      new filename
# return: return 0 if rename successfully,  otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Rename
cbd.Rename(user, "/curve", "/curve-new")
```

#### Create a directory

```python
# parameter: the two parameters are
#      directory path
#      user information
# return: return 0 if create successfully,  otherwise return an error code

# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"
# Create directory
cbd.Mkdir("/curvedir", user)
```

#### Delete directory

```python
# parameter: the two parameters are
#      directory path
#      user information
# return: return 0 if create successfully,  otherwise return an error code
  
# Construct user information
user = curvefs.UserInfo_t()
user.owner = "curve"

# Delete directory
cbd.Rmdir("/curvedir", user)
```

#### List the files in the directory

```python
# parameter: the two parameters are
#      directory path
#      user information
# return: files in the directory (only filename)
files = cbd.Listdir("/test", user)
for f in files:
    print f
```

#### Get the cluster ID

```python
# Determine whether this get is successful by the return value
# success: return cluster ID
# fail: return null string
clusterId = cbd.GetClusterId()
print clusterId
# c355675a-f4d2-4729-b80a-5a7bcc749d1c
```

#### Clean CBDClient object

```python
cbd.UnInit()
```

### Error code

| Code | Message                     | description                     |
| :--: | :-------------------------- | ------------------------ |
|  0   | OK                          | success                 |
|  -1  | EXISTS                      | file or directory exists       |
|  -2  | FAILED                      | fail                 |
|  -3  | DISABLEDIO                  | disable io                   |
|  -4  | AUTHFAIL                    | authentication failed                 |
|  -5  | DELETING                    | deleting                 |
|  -6  | NOTEXIST                    | file not exist               |
|  -7  | UNDER_SNAPSHOT              | under snapshot                   |
|  -8  | NOT_UNDERSNAPSHOT           | not under snapshot               |
|  -9  | DELETE_ERROR                | delete error                 |
| -10  | NOT_ALLOCATE                | segment not allocated           |
| -11  | NOT_SUPPORT                 | operation not supported               |
| -12  | NOT_EMPTY                   | directory not empty                 |
| -13  | NO_SHRINK_BIGGER_FILE       | no shrinkage                 |
| -14  | SESSION_NOTEXISTS           | session not exist           |
| -15  | FILE_OCCUPIED               | file occupied               |
| -16  | PARAM_ERROR                 | parameter error                 |
| -17  | INTERNAL_ERROR              | internal error                |
| -18  | CRC_ERROR                   | CRC error              |
| -19  | INVALID_REQUEST             | parameter invalid         |
| -20  | DISK_FAIL                   | disk fail                 |
| -21  | NO_SPACE                    | no space                 |
| -22  | NOT_ALIGNED                 | io not aligned                 |
| -23  | BAD_FD                      | file is being closed, fd unavailable |
| -24  | LENGTH_NOT_SUPPORT          | file length is not supported       |
| -25  | SESSION_NOT_EXIST           | session not exist, duplicate with -14 |
| -26  | STATUS_NOT_MATCH            | status error                 |
| -27  | DELETE_BEING_CLONED         | delete the file being cloned       |
| -28  | CLIENT_NOT_SUPPORT_SNAPSHOT | this version of client not support snapshot   |
| -29  | SNAPSHOT_FROZEN             | snapshot is disabled       |
| -100 | UNKNOWN                     | unknown error                 |