Friday, 28 April 2023

Use temporary Blob storage for accessing files during runtime

We can learn about how to store a file in the temporary blob storage, so that the file can be used during runtime process for our scenarios. I have a scenario, where I need to get the company image and use it in my process.

In order to do this, we can get the image file from DB and upload it to the temporary Blob storage. Further we can get the URL for the uploaded file and that can be used to send or download the image as required in our process.

Below code sample help us for the scenario.

Image                             logoImage;
Binary                             binary;
System.IO.Stream        imageStream;
str                                   downloadUrl;
       
logoImage = new Image();
logoImage.setData(CompanyImage::findByRecord(CompanyInfo::find()).Image);
 
binary = Binary::constructFromContainer(logoImage.getData());
 
imageStream = binary.getMemoryStream();
 
if (imageStream)
{
    str fileName        = 'Your file name'; //Fixed name or GUID can be created for dynamic name
    str contentType  = 'image/png'; // Change according to your file type
    str fileExtension = enum2Str(ImageType::PNG); // Change according to your file type
 
    FileUploadTemporaryStorageStrategy fileUploadStrategy = new FileUploadTemporaryStorageStrategy();
    FileUploadTemporaryStorageResult fileUploadResult = fileUploadStrategy.uploadFile(imageStream, fileName + fileExtension, contentType, fileExtension);
 
    if (fileUploadResult == null || !fileUploadResult.getUploadStatus())
    {
        warning("@ApplicationPlatform:FileUploadFailed");
    }
    else
    {
        downloadUrl = fileUploadResult.getDownloadUrl();
        if (downloadUrl == "")
        {
            throw Exception::Error;
        }
    }
}

The downloadUrl hold the link for the temporarily stored file path. We can use this in our next process as required.





No comments:

Post a Comment