Content

Learn how to store and access files with QuickBlox file storage.

The content module allows storing rich chat attachments, app content, and settings without having to republish them. Using a web interface you or your clients can control and make instant changes to the apps.

Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.

Before you begin

  1. Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
  2. Configure QuickBlox SDK for your app. Check out our Setup page for more details.
  3. Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.

Retrieve files

Get a list of files for a current user using the code snippet below.

let page = QBGeneralResponsePage(currentPage: 1, perPage: 20)
QBRequest.blobs(for: page, successBlock: { (response, page, blobs) in
    
}, errorBlock: { (response) in
    
})
QBGeneralResponsePage *page = [QBGeneralResponsePage responsePageWithCurrentPage:1 perPage:20];

[QBRequest blobsForPage:page successBlock:^(QBResponse *response, QBGeneralResponsePage *page, NSArray *blobs) {
    
} errorBlock:^(QBResponse *response) {
    
}];

Upload file

Upload a file to the cloud using the following code snippet.

// your file - this is an image in our case
guard let imageData = myImage?.pngData() else {
    return
}
QBRequest.tUploadFile(imageData, fileName: "myImage", contentType: "image/png", isPublic: true, successBlock: { (response, uploadedBlob) in
    
}, statusBlock: { (request, status)  in
    // Update UI with upload progress
}, errorBlock: { (response) in
    
})
// your file - this is an image in our case
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"arrow.png"]);

[QBRequest TUploadFile:imageData fileName:@"arrow.png" contentType:@"image/png" isPublic:NO successBlock:^(QBResponse *response, QBCBlob *blob) {
   
} statusBlock:^(QBRequest *request, QBRequestStatus *status) {
    // Update UI with upload progress
} errorBlock:^(QBResponse *response) {
    
}];

The maximum size of the uploaded file depends on the membership plan.

BasicStartupGrowthHIPAAEnterprise
File size limit10 Mb25 Mb50Mb50MbContact our sales team

Update file

Update a previously uploaded file in the cloud using the tUpdateFile(with:file:) method below. Set the isNew field as true if you want to update a file.

// your new file - this is an image in our case
guard let newImageData = myNewImage?.pngData() else {
    return
}

let existingBlob: QBCBlob = QBCBlob() // previously received Blob
existingBlob.isNew = true // set as true if you want to update blob's file.

QBRequest.tUpdateFile(with: newImageData, file: existingBlob, successBlock: { (response) in
    
}, statusBlock: { (request, status)  in
    // update UI with upload progress
}, errorBlock: { (response) in
    
})
// your new file - this is an image in our case
NSData *newImageData = UIImagePNGRepresentation([UIImage imageNamed:@"arrow.png"]);

QBCBlob * existingBlob = [[QBCBlob alloc] init]; // previously received Blob
existingBlob.isNew = YES; // set as YES if you want to update blob's file

[QBRequest TUpdateFileWithData:newImageData file:existingBlob successBlock:^(QBResponse * _Nonnull response) {
    
} statusBlock:^(QBRequest *request, QBRequestStatus *status) {
    // update UI with upload progress
} errorBlock:^(QBResponse *response) {
    
}];
ArgumentRequiredDescription
newImageDataYesA new file in NSData format.
existingBlobYesA previously obtained blob with
a file that needs to be updated.

Download file by UID

If the file is public then it's possible to download it without a session token.

let uid = "d816966db53640e68b304a3cd4e5c0c100"

QBRequest.downloadFile(withUID: uid, successBlock: { (response, fileData)  in
    
}, statusBlock: { (request, status) in
    let progress = CGFloat(status.percentOfCompletion)
}, errorBlock: { (response) in
    
})
NSString *uid = @"d816966db53640e68b304a3cd4e5c0c100";

[QBRequest downloadFileWithUID:uid successBlock:^(QBResponse *response, NSData *fileData) {
    
} statusBlock:^(QBRequest *request, QBRequestStatus *status) {
    NSLog(@"download progress: %f", status.percentOfCompletion);
} errorBlock:^(QBResponse *response) {
    
}];

Get file info

Get information about a file by ID using the method below. This method allows to load the file description model from the server, not the content of the file itself.

QBRequest.blob(withID: 1441441, successBlock: { (response, blob) in
    // content type in mime format.
    let blobContentType = blob.contentType
    
    // file name.
    let blobName = blob.name;
    
    // status of the File.
    let blobStatus = blob.status;
    
    // date when the file upload has been completed.
    let blobCompletedAt = blob.completedAt;
    
    // the size of file in bytes, readonly
    let blobSize = blob.size;
    
    // file unique identifier.
    let blobUID = blob.uid;
    
    // last read file time.
    let blobLastReadAccessTs = blob.lastReadAccessTs;
    
    // an instance of BlobObjectAccess.
    let blobObjectAccess = blob.blobObjectAccess;
    
    // coma separated string with file's tags.
    let blobTags = blob.tags;
    
    // file's visibility.
    let blobIsPublic = blob.isPublic;
    
    // set as YES if you want to update blob's file.
    let blobIsNew = blob.isNew;
    
}, errorBlock: { (response) in
    
})
[QBRequest blobWithID:1441441 successBlock:^(QBResponse * _Nonnull response, QBCBlob * _Nonnull blob) {
    
    // content type in mime format.
    NSString *blobContentType = blob.contentType;
    
    // file name.
    NSString *blobName = blob.name;
    
    // status of the File.
    QBCBlobStatus blobStatus = blob.status;
    
    // date when the file upload has been completed.
    NSDate *blobCompletedAt = blob.completedAt;
    
    // the size of file in bytes, readonly
    NSUInteger blobSize = blob.size;
    
    // file unique identifier.
    NSString *blobUID = blob.UID;
    
    // last read file time.
    NSDate *blobLastReadAccessTs = blob.lastReadAccessTs;
    
    // an instance of BlobObjectAccess.
    QBCBlobObjectAccess *blobObjectAccess = blob.blobObjectAccess;
    
    // coma separated string with file's tags.
    NSString *blobTags = blob.tags;
    
    // file's visibility.
    BOOL blobIsPublic = blob.isPublic;
    
    // set as YES if you want to update blob's file.
    BOOL blobIsNew = blob.isNew;
    
} errorBlock:^(QBResponse *response) {
    
}];
ArgumentRequiredDescription
fileIdyesID of the file/blob.
successBlockyesSpecifies a response block that is called if the request is succeeded. As a result, the response will contain a blob/file description model.
errorBlockyesSpecifies a response block that is called in case an error is occurred.

Get file URL

There are two types of file URLs that can be obtained: private and public.

  • Public URL allows anyone to access the file, no authorization token is needed.
  • Private URL can be accessed only by QuickBlox user with a session token.

Get public URL

To receive a public URL, use the code snippet below.

let file: QBCBlob = ...
let publicUrl = file.publicUrl()

// or if you have only file UID
let fileUID = "6221dd49a1bb46cfb61efe62c4526bd800"
let publicUrl = QBCBlob.publicUrl(forFileUID: fileUID)
QBCBlob *file = ...;
NSString *publicUrl = [file publicUrl];
 
// or if you have only file UID
NSString *fileUID = @"6221dd49a1bb46cfb61efe62c4526bd800";
NSString *publicUrl = [QBCBlob publicUrlForFileUID:fileUID];
ArgumentRequiredDescription
fileUIDyesFile unique identifier. Type String

Get private URL

To get a private URL of the uploaded file, use the following code snippet.

let file: QBCBlob = ...
let privateUrl = file.privateUrl()

// or if you have only file UID
let fileUID = "6221dd49a1bb46cfb61efe62c4526bd800"
let privateUrl = QBCBlob.privateUrl(forFileUID: fileUID)
QBCBlob *file = ...;
NSString *privateUrl = [file privateUrl];
 
// or if you have only file UID
NSString *fileUID = @"6221dd49a1bb46cfb61efe62c4526bd800";
NSString *privateUrl = [QBCBlob privateUrlForFileUID:fileUID];
ArgumentRequiredDescription
fileUIDyesFile unique identifier. Type String

Delete file

Delete a file by file ID using the deleteBlob() method.

QBRequest.deleteBlob(withID: 1441441, successBlock: { (response) in
    
}, errorBlock: { (response) in
    
})
[QBRequest deleteBlobWithID:1441441 successBlock:^(QBResponse * _Nonnull response) {
    
} errorBlock:^(QBResponse * _Nonnull response) {
   
}];
ArgumentRequiredDescription
fileIdyesID of the file/blob.
successBlockyesSpecifies a response block that is called if the request is succeeded.
errorBlockyesSpecifies a response block that is called in case an error is occurred.

What’s Next