File Management Service
Access info
Endpoint | https://file-management-api.socrate.io/graphql |
Required access keys | Tenant-level service access key |
Pricing | Please contact us for details at contact@bitsoftware.ro |
Notes | To call the service, the access key must be provided in the x-api-key header of the HTTP request. If you are using the GraphQL console, you can view the service’s documentation and schema only after entering an access key. Make sure that the scope of the key allows access to the queries and mutations that you require. For example, to grant the key access to all queries and mutations, the keys’s scope must be set to file-management-api:query:* file-management-api:mutation:* . |
Usage
The File Management Service enables managing files in the cloud (create, update, delete). You can create the file content either by downloading it from a publicly accessible URI or by uploading the file content separately through an HTTP PUT
method.
PUT
method. Creating file content from a public URI is not yet supported.
Files can be publicly accessible or protected. In case of protected files, you can generate a temporary download URL that will expire after a custom number of seconds.
The API also provides the ability to filter records, and add custom file attributes.
Creating files
Creating new files is a two-step operation. First, run the createFile
mutation and specify the file name to be created as input, for example:
mutation create_file($input:CreateFileInput) {
createFile(input: $input) {
id
uploadUrl(expiresIn:3600)
}
}
{
"input": {
"name": "info.pdf"
}
}
The mutation above creates a file and returns the file’s upload URL. The latter is required for the next step. Note that the period after which the upload URL will expire can be parameterized according to your needs. In the mutation above, it is 3600 seconds (1 hour).
Secondly, run an HTTP PUT
request and upload the actual file bytes to the upload URL. The exact program code to achieve this depends on the language you are using. With cURL, the upload operation looks as follows:
- If you have a bash terminal, run:
curl --location --request PUT 'YOUR_UPLOAD_URL' --header 'Content-Type: application/pdf' --data-binary '@/path/to/your/file.pdf'
- If you have a Windows terminal, run:
curl --location --request PUT "YOUR_UPLOAD_URL" --header "Content-Type: application/pdf" --data-binary "@C:\file.pdf"
Notes:
- In the command above, make sure to adjust the upload URL and the path to your local file.pdf as required.
- Supplying the
Content-Type
HTTP header is mandatory. In the listing above, the content (MIME) type is applicable to PDF files. For a list of common MIME types, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types. - You can optionally add the
Content-Disposition
HTTP header to the request, which will provide indication to the browser, at file download time, how it should treat the content (inline or as attachment). See also “Content-Disposition” header.
For a more detailed example, see the Tutorial: Create a file.
Renaming files
To rename a file, run the updateFile
mutation. You will need to supply as input the file ID and the new file name (with extension), for example:
mutation($input:UpdateFileInput) {
updateFile(input: $input) {
name
}
}
{
"input": {
"id": "YOUR_FILE_ID",
"name": "renamedFile.txt"
}
}
Updating files
You can update existing files by issuing a new HTTP PUT
request to the file’s upload URL, with the new file bytes in the payload of the request. To obtain the file’s upload URL, run the files
query and make sure to set the URL’s expiry timeout to a suitable value for your needs, for example:
query($filter:FilesFilter, $uploadUrlTimeout:Int) {
files(filter:$filter){
items {
id
name
status
uploadUrl(expiresIn: $uploadUrlTimeout)
}
}
}
{
"filter": {
"id": "YOUR_FILE_ID"
},
"uploadUrlTimeout": 3600
}
While the upload URI is still valid, run an HTTP PUT
request to replace the file content. This request looks like the one illustrated above for creating new files.
Note that you can change the actual file’s bytes but you cannot change the file content (MIME) type (for example, from text to PDF). To achieve that, you will need to create a new file.
Deleting files
To delete a file, run the deleteFile
mutation. You will need to supply as input the file ID of the file to be deleted, for example:
mutation delete_file($id: String) {
deleteFile(id: $id) {
id
}
}
{
"id": "YOUR_FILE_ID"
}
Uploading large files
When you need to upload large files exceeding 100 MB, you should consider the multipart upload. This means uploading the file in smaller chunks, rather than performing the upload as one operation. To perform a multipart upload, the API provides dedicated mutations. More precisely, the steps for uploading a large file are as follows:
- Programmatically split the file into several file parts (byte arrays). Each part must be larger than 5 MB and less than 5 GB. There is no minimum size limit on the last part.
- Call
initiateMultipartUpload
to obtain a unique identifier of the upload. - For each individual part, call
generatePartUploadUrl
to get its upload URL. - To upload each individual part, send a
PUT
request at the obtained upload URL. When parts are uploaded, the response contains anETag
header. This value will be required in thecompleteMultipartUpload
mutation. - Run the
completeMultipartUpload
mutation to assemble the uploaded file parts back into the original file.
For an example, see the Tutorial: Upload a large file.
“Content-Disposition” header
Except for files to which you haven’t uploaded any content yet, all files created through the File Management Service have a download URL. When any browser or HTTP client accesses the URL, the HTTP response is by default without a Content-Disposition
Header. This means the browser does not have a clear indication on whether to open the download URL inline (that is, directly in the browser window) or as an attachment. You can optionally set the Content-Disposition
header (and thus define explicitly how the browser should treat the download URL) by adding this header to the HTTP PUT request when you upload the file content.
For example, setting the Content-Disposition
header as follows:
Content-Disposition: attachment; filename="start.jpg"
will indicate to the browser (when downloading the file) that the file is a downloadable resource, and also provide a hint that the suggested file name for saving is “start.jpg”.
Queries
file
Retrieve information about a single file, by file ID.
Arguments
Argument | Type | Description |
---|---|---|
ID | ID! |
Mandatory. The ID of the file to be retrieved. |
Result
See the File
type.
files
Lists files from the cloud, according to the filter criteria provided as input.
Arguments
Argument | Type | Description |
---|---|---|
filter | FilesFilter |
Optional argument used to filter files. |
nextToken | String |
Optional argument used to fetch the next set of query results. This value can be obtained from the nextToken attribute of the FilesResult type. |
FilesFilter
input
Attribute | Type | Description |
---|---|---|
id | String |
Specifies the ID of the file by which to filter. Note that, if both id (this parameter) and ids (the next parameter) are specified, then ids are ignored. |
ids | [String] |
Specifies an array of file IDs by which to filter. |
active | Boolean |
Optional. True (default) - filter only active files; False - filter deactivated files. This field is currently not in use. |
Result
FilesResult
type
Attribute | Type | Description |
---|---|---|
items | File |
An array of File objects. |
nextToken | String |
If null, then the query has reached the end of the list of files that match the query criteria. If not null, then use this value in the nextToken input argument to obtain the next page. |
File
type
Attribute | Type | Description |
---|---|---|
id | ID |
The unique permanent file identifier. |
name | String |
The file name. |
active | Boolean |
True if the file is active; false if the file is deactivated. This field is currently not in use. |
comment | String |
A human readable comment. |
author | Author |
Provides information about the author of the last change. |
contentType | String |
The file content (MIME) type. |
contentLength | Float |
The file size in bytes. |
status | FileStatus |
Provides information about the current state of the file. |
error | Error |
If status is “error”, this field will contain the corresponding error code and message. |
createdAt | DateTime |
The date and time when the file was created. |
updatedAt | DateTime |
The date and time when the file was last updated. |
externalReference | String |
An identifier used to associate the file with an entity from the caller system. |
uploaded | Boolean |
Indicates that there is file content present, perhaps from some previous upload. |
public | Boolean |
Indicates that access to the file is public (true) or protected (false). |
downloadUrl | String |
Returns the URL from which the file can be downloaded. The optional argument expiresIn of type Int specifies the time period, in seconds, after which the URL will expire (applicable only to protected access). |
uploadUrl | String |
Returns an upload URL at which you can replace the content of the file through a PUT HTTP request. The optional argument expiresIn of type Int specifies the time period, in seconds, after which the URL will expire (default is 30 seconds). |
attributes | Attribute |
A list of custom attributes defined as name-value pairs. Used to store extra information pertaining to the file. |
Author
type
Attribute | Type | Description |
---|---|---|
id | ID |
The author’s unique permanent identifier. |
name | String |
The author’s name. |
String |
The author’s email. | |
profile | String |
The author’s profile (for example, “admin”, “user”, “customer”, etc). |
FileStatus
enum
Value | Description |
---|---|
uploading | File content is in the process of being uploaded. |
downloading | File content is in the process of being downloaded from the provided URI. |
done | The file is ready. |
error | An error has occurred during upload or transfer. |
Error
type
Attribute | Type | Description |
---|---|---|
code | String |
The error code. |
message | String |
The error message. |
Attribute
type
Attribute | Type | Description |
---|---|---|
name | String |
The attribute’s name. Must be unique per file. |
value | String |
The attribute’s value. |
filesWithExternalReference
Lists only files having the externalReference
attribute supplied as input.
Arguments
Argument | Type | Description |
---|---|---|
externalReference | String! |
Mandatory. Only files having this externalReference value will be retrieved. |
filter | FilesWithExternalReferenceFilter |
Optional argument used to filter the results. |
FilesWithExternalReferenceFilter
input
Attribute | Type | Description |
---|---|---|
status | FileStatus |
If this filter is provided, only files with the respective status will be retrieved. |
Result
An array of objects of File
type.
version
Returns the API version.
Mutations
createFile
Creates a file in the cloud. Depending on the input arguments, the file content is either empty or populated from some URI.
Arguments
Argument | Type | Description |
---|---|---|
input | CreateFileInput |
Optional argument used to define the properties of the new file, such as the file name. |
CreateFileInput
input
Attribute | Type | Description |
---|---|---|
name | String |
Optional. Specifies the name of the file as it is in the file system, with an extension, if applicable. |
contentType | String |
Optional. Specifies the content (MIME) type of the file. If omitted, this will be retrieved from the file extension. For the list of official MIME types, see https://www.iana.org/assignments/media-types/media-types.xhtml. |
comment | String |
Optional. A human-readable comment. |
author | AuthorInput |
Optional. Identifies the author of the file. |
externalReference | String |
Optional. Specifies the identifier of the file in the calling system. |
fileSourceUri | String |
Optional. Set this parameter if the file should be created from a publicly accessible URI. If this parameter is set, the file will be downloaded from the provided URI. Valid protocols are HTTP and HTTPS. |
public | Boolean |
Optional. Set this parameter to make the file public (true) or protected (false). The default value is false. |
attributes | AttributeInput |
Optional. A list of custom attributes defined as name-value pairs. Used to store extra information pertaining to the file. |
AuthorInput
input
Attribute | Type | Description |
---|---|---|
id | ID |
Optional. The author’s unique permanent identifier. |
name | String |
Optional. The author’s name. |
String |
Optional. The author’s email. | |
profile | String |
Optional. The author’s profile (for example, “admin”, “user”, “customer”, etc). |
AttributeInput
input
Attribute | Type | Description |
---|---|---|
name | String |
Mandatory. The attribute’s name. Must be unique per file. |
value | String |
Optional. The attribute’s value. |
Result
File
type
See the File
type.
completeMultipartUpload
This mutation is useful when uploading large files, through a multipart upload. It completes the multipart upload operation, by assembling the already uploaded file parts back into a file. Run this mutation only after you have run the initiateMultipartUpload
and generatePartUploadUrl
mutations.
Arguments
Argument | Type | Description |
---|---|---|
input | completeMultipartUploadInput! |
Provides the input to the mutation. |
completeMultipartUploadInput
input
Attribute | Type | Description |
---|---|---|
fileId | String! |
Mandatory. The ID of a file created through the File Management Service. |
uploadId | String! |
Mandatory. The ID of the upload obtained as a result of running the initiateMultipartUpload mutation. |
etags | [PartETag!] |
Mandatory. An array of PartETag objects. |
PartETag
input
Attribute | Type | Description |
---|---|---|
partNumber | Int! |
Mandatory. The part number of an uploaded file part. |
ETag | String! |
Mandatory. The value returned in the ETag HTTP response header after uploading a file part. |
Result
File
type
See the File
type.
deleteFile
Arguments
Argument | Type | Description |
---|---|---|
id | String |
The ID of the file to be deleted. |
Result
File
type
See the File
type.
generatePartUploadUrl
This mutation is useful when uploading large files, through a multipart upload. It returns an upload URL at which a file part can be uploaded. Run this mutation only after you have run the initiateMultipartUpload
and have obtained an upload ID as a result. This mutation must be run as many times as there are file parts.
Arguments
Argument | Type | Description |
---|---|---|
input | generatePartUploadUrlInput! |
Provides the input to the mutation. |
generatePartUploadUrlInput
input
Attribute | Type | Description |
---|---|---|
fileId | String! |
Mandatory. The ID of a file created through the File Management Service. |
uploadId | String! |
Mandatory. The ID of the upload obtained as a result of running the initiateMultipartUpload mutation. |
partNumber | Int! |
Mandatory. The identifier of the part for which the upload URL is to be generated. |
expiresIn | Int |
Optional. Number of seconds after which the upload URL will expire. |
Result
The result is a string that contains the generated upload URL.
initiateMultipartUpload
Initiates a multipart upload of the file. This mutation is the first one in the series of mutations required to upload a large file. It returns an upload identifier that is to be subsequently used in generatePartUploadUrl
and completeMultipartUpload
mutations.
Arguments
Argument | Type | Description |
---|---|---|
input | initiateMultipartUploadInput! |
Provides the input to the mutation. |
initiateMultipartUploadInput
input
Attribute | Type | Description |
---|---|---|
fileId | String |
The ID of a file created through the File Management Service. |
Result
MultipartUpload
type
Attribute | Type | Description |
---|---|---|
uploadId | String |
The ID of the upload to be used in subsequent mutations. |
updateFile
Arguments
UpdateFileInput
input
Attribute | Type | Description |
---|---|---|
id | ID |
The ID of the file to be updated. |
name | String |
Optional. Specifies the name of the file as it is in the file system, with an extension, if applicable. |
contentType | String |
Optional. Specifies the content (MIME) type of the file. If omitted this will be retrieved from the file extension. |
comment | String |
Optional. A human-readable comment. |
author | AuthorInput |
Optional. Identifies the author of the file. |
externalReference | String |
Optional. Specifies the identifier of the file in the calling system. |
fileSourceUri | String |
Optional. Set this parameter if the file should be created from a publicly accessible URI. If this parameter is set, the file will be downloaded from the provided URI. Valid protocols are HTTP and HTTPS. |
public | Boolean |
Optional. Set this parameter to make the file public (true) or protected (false). The default value is false. |
attributes | AttributeInput |
Optional. A list of custom attributes defined as name-value pairs. Used to store extra information pertaining to the file. |
Result
File
type
See the File
type.