Skip to main content

File Uploads

When you upload a file, Wildix CDN becomes the host for your file, taking charge of its safe storage. Uploading files is by far the easiest way to use files in x-bees.

  • Files are stored in company storage and could be removed due to policy configured in WMS.
  • Files are attached to a specific channel, and access to the file is available only to members of the channel.
  • Each message can have up to 10 attachments.
  • Once a file is uploaded, it cannot be removed, but the access to the file can be restricted if you remove the message where the file is attached.
  • If a service token is used, it is necessary to specify the ID of the user on whose behalf the operation is performed.

Commands

Retrieves a presigned URL for downloading a specific file from a message in a specified channel.
Retrieves information about an uploaded file in a specified channel.
Create a presigned URL for file upload in the channel.

Upload files or images

import {
ConversationsClient,
GetUploadedFileInfoCommand,
SendMessageCommand,
UploadFileCommand
} from '@wildix/xbees-conversations-client';
import fs from 'fs';

// Initialize the client with the desired configuration
const client = new ConversationsClient({...});

const channelId = '<channel-id>';
const fileName = 'example.jpg';
const fileContent = fs.readFileSync(`./${fileName}`);

// Create a file resource to get an upload URL for the file
const fileUploadResponse = await client.send(
new UploadFileCommand({
channelId,
name: fileName,
// Specify user ID in case of service authorization
options: {
userId: '<user-id>',
},
}),
);

// Upload content to the file storage service
await fetch(fileUploadResponse.presignedUploadUrl, {
method: 'PUT',
body: fileContent,
});

// Get the uploaded file info
const fileUploadInfoResponse = await client.send(
new GetUploadedFileInfoCommand({
channelId,
fileId: fileUploadResponse.fileId,
}),
);

// Prepare attachments for the message
const attachments = [];
if (fileUploadInfoResponse.file) {
attachments.push(fileUploadInfoResponse.file);
}

// Send a message with the uploaded file attached
const sendMessageResponse = await client.send(
new SendMessageCommand({
channelId,
userId: '<user-id>', // Specify user ID in case of service authorization
text: "File example",
attachments,
}),
);

console.log('Message sent successfully', sendMessageResponse);

Download files

import {
ConversationsClient,
GetFilePresignedDownloadUrlCommand
} from '@wildix/xbees-conversations-client';

// Initialize the client with the desired configuration
const client = new ConversationsClient({...});

const channelId = '<channel-id>';
const messageId = '<message-id>';
const fileId = '<file-id>';

// Get the presigned download URL for the file
const fileDownloadUrlResponse = await client.send(
new GetFilePresignedDownloadUrlCommand({
channelId,
messageId,
fileId,
}),
);

// Fetch the file content from the presigned URL
const fileDownloadResponse = await fetch(fileDownloadUrlResponse.presignedDownloadUrl);
const fileDownloadBuffer = await fileDownloadResponse.arrayBuffer();

console.log('File downloaded successfully', fileDownloadBuffer);