# Project import/export API

> 原文：[https://docs.gitlab.com/ee/api/project_import_export.html](https://docs.gitlab.com/ee/api/project_import_export.html)

*   [Schedule an export](#schedule-an-export)
*   [Export status](#export-status)
*   [Export download](#export-download)
*   [Import a file](#import-a-file)
*   [Import status](#import-status)

# Project import/export API[](#project-importexport-api "Permalink")

在 GitLab 10.6 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41899) .

也可以看看：

*   [Project import/export documentation](../user/project/settings/import_export.html).
*   [Project import/export administration Rake tasks](../administration/raketasks/project_import_export.html).

## Schedule an export[](#schedule-an-export "Permalink")

开始新的导出.

端点还接受`upload`参数. 此参数是一个哈希，其中包含将导出的项目上载到 Web 服务器或任何 S3 兼容平台的所有必要信息. 目前，我们仅支持将二进制数据文件上传到最终服务器.

从 GitLab 10.7 开始，如果存在`upload`参数，则需要`upload[url]`参数.

```
POST /projects/:id/export 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) |
| `description` | string | no | 覆盖项目描述 |
| `upload` | hash | no | Hash that contains the information to upload the exported project to a web server |
| `upload[url]` | string | yes | 上传项目的网址 |
| `upload[http_method]` | string | no | 上传输出项目的 HTTP 方法. 仅允许使用`PUT`和`POST`方法. 默认为`PUT` |

```
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/export" \
    --data "upload[http_method]=PUT" \
    --data-urlencode "upload[url]=https://example-bucket.s3.eu-west-3.amazonaws.com/backup?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIMBJHN2O62W8IELQ%2F20180312%2Feu-west-3%2Fs3%2Faws4_request&X-Amz-Date=20180312T110328Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=8413facb20ff33a49a147a0b4abcff4c8487cc33ee1f7e450c46e8f695569dbd" 
```

```
{  "message":  "202 Accepted"  } 
```

**注意：**上载请求将与`Content-Type: application/gzip`标头一起发送. 确保您的预签名 URL 将此内容作为签名的一部分.

## Export status[](#export-status "Permalink")

获取导出状态.

```
GET /projects/:id/export 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/export" 
```

状态可以是以下之一：

*   `none`
*   `queued`
*   `started`
*   `finished`
*   `regeneration_in_progress`

`queued`状态表示已接收到导出请求，并且当前处于要处理的队列中.

`started`状态表示导出过程已开始并且当前正在进行中. 它包括导出过程，对生成的文件执行的操作，例如发送电子邮件通知用户下载文件，将导出的文件上传到 Web 服务器等.

`finished`状态是在导出过程完成并且已通知用户之后.

`regeneration_in_progress`是可以下载导出文件且正在处理生成新导出文件的请求.

`_links`仅在导出完成时存在.

```
{  "id":  1,  "description":  "Itaque perspiciatis minima aspernatur corporis consequatur.",  "name":  "Gitlab Test",  "name_with_namespace":  "Gitlab Org / Gitlab Test",  "path":  "gitlab-test",  "path_with_namespace":  "gitlab-org/gitlab-test",  "created_at":  "2017-08-29T04:36:44.383Z",  "export_status":  "finished",  "_links":  {  "api_url":  "https://gitlab.example.com/api/v4/projects/1/export/download",  "web_url":  "https://gitlab.example.com/gitlab-org/gitlab-test/download_export",  }  } 
```

## Export download[](#export-download "Permalink")

下载完成的导出.

```
GET /projects/:id/export/download 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" --remote-header-name --remote-name "https://gitlab.example.com/api/v4/projects/5/export/download" 
```

```
ls *export.tar.gz
2017-12-05_22-11-148_namespace_project_export.tar.gz 
```

## Import a file[](#import-a-file "Permalink")

```
POST /projects/import 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `namespace` | integer/string | no | 项目将导入到的名称空间的 ID 或路径. 默认为当前用户的名称空间 |
| `name` | string | no | 要导入的项目的名称. 如果未提供，则默认为项目的路径 |
| `file` | string | yes | 要上传的文件 |
| `path` | string | yes | 新项目的名称和路径 |
| `overwrite` | boolean | no | 如果存在具有相同路径的项目，则导入将覆盖它. 默认为假 |
| `override_params` | Hash | no | 支持在[Project API 中](projects.html)定义的所有字段 |

传递的覆盖参数将优先于导出文件中定义的所有值.

要从文件系统上载文件，请使用`--form`参数. 这将导致 cURL 使用标题`Content-Type: multipart/form-data` . `file=`参数必须指向文件系统上的文件，并以`@`开头. 例如：

```
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" --form "path=api-project" --form "file=@/path/to/file" "https://gitlab.example.com/api/v4/projects/import" 
```

cURL 不支持从远程服务器发布文件. 从远程服务器导入项目可以通过以下方式完成：

```
import requests
from io import BytesIO

s3_file = requests.get(presigned_url)

url =  'https://gitlab.example.com/api/v4/projects/import'
files = {'file': ('file.tar.gz', BytesIO(s3_file.content))}
data = {
    "path": "example-project",
    "namespace": "example-group"
}
headers = {
    'Private-Token': "<your_access_token>"
}

requests.post(url, headers=headers, data=data, files=files) 
```

```
{  "id":  1,  "description":  null,  "name":  "api-project",  "name_with_namespace":  "Administrator / api-project",  "path":  "api-project",  "path_with_namespace":  "root/api-project",  "created_at":  "2018-02-13T09:05:58.023Z",  "import_status":  "scheduled",  "correlation_id":  "mezklWso3Za",  "failed_relations":  []  } 
```

**注意：**可以由管理员设置最大导入文件大小，默认为 50MB. 作为管理员，您可以修改最大导入文件大小. 为此，请在" [应用程序设置" API](settings.html#change-application-settings)或" [管理界面"中](../user/admin_area/settings/account_and_limit_settings.html)使用`max_import_size`选项.

## Import status[](#import-status "Permalink")

获取导入状态.

```
GET /projects/:id/import 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding) |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/import" 
```

状态可以是以下之一：

*   `none`
*   `scheduled`
*   `failed`
*   `started`
*   `finished`

如果状态`failed` ，它将在`import_error`下包含导入错误消息. 如果状态为`failed` ， `started`或`finished`时， `failed_relations`阵列可能与关系的任何事件未能导入无论是由于不可恢复的错误或因重试次数已经用尽被填充（一个典型的例子是查询超时.）

**注意：** `failed_relations`的元素的`id`字段引用失败记录，而不是关系.**注意：** `failed_relations`数组当前限制为 100 个项目.

```
{  "id":  1,  "description":  "Itaque perspiciatis minima aspernatur corporis consequatur.",  "name":  "Gitlab Test",  "name_with_namespace":  "Gitlab Org / Gitlab Test",  "path":  "gitlab-test",  "path_with_namespace":  "gitlab-org/gitlab-test",  "created_at":  "2017-08-29T04:36:44.383Z",  "import_status":  "started",  "correlation_id":  "mezklWso3Za",  "failed_relations":  [  {  "id":  42,  "created_at":  "2020-04-02T14:48:59.526Z",  "exception_class":  "RuntimeError",  "exception_message":  "A failure occurred",  "source":  "custom error context",  "relation_name":  "merge_requests"  }  ]  } 
```