You can quickly upload files from your local device to the cloud. After the upload is complete, you can go to the Cloud Storage page of AppGallery Connect to view the uploaded files.
The size of a single file to be uploaded cannot exceed 1 GB.
This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.
Import required modules.
import { cloudStorage } from '@kit.CloudFoundationKit';
// ...
import { request } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { GlobalContext } from '../common/GlobalContext';
import { fileIo } from '@kit.CoreFileKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit'; Upload a file.
Select the file to be uploaded. In the following sample code, photoAccessHelper.PhotoViewPicker is used to specify the file to be uploaded.
Copy the file to be uploaded to the context.cacheDir directory.
The input parameter localPath of the StorageBucket.uploadFile API can only be set to a file path in the context.cacheDir directory. Therefore, you need to copy the file to the context.cacheDir directory before uploading the file.
Call the StorageBucket.uploadFile API to create an upload task and listen for the progress, completed, and failed events of the upload task.
Start the upload task.
The complete sample code is as follows:
hilog.info(0x0000, 'Storage', `upload file with api`);
let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.maxSelectNumber = 1;
let photoViewPicker = new photoAccessHelper.PhotoViewPicker();
photoViewPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
let fileUri = photoSelectResult.photoUris[0];
hilog.info(0x0000, 'Storage', `pick file ${fileUri}`);
let fileName = fileUri.split('/').pop() as string;
UI.uploadFileName = fileName;
hilog.info(0x0000, 'Storage', `file name ${fileName}`);
let cacheFile = `${Date.now()}_${fileName}`;
hilog.info(0x0000, 'Storage', `cacheFile ${cacheFile}`);
let cacheFilePath = GlobalContext.getContext().cacheDir + '/' + cacheFile;
try {
let srcFile = fileIo.openSync(fileUri);
let dstFile = fileIo.openSync(cacheFilePath, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
fileIo.copyFileSync(srcFile.fd, dstFile.fd);
fileIo.closeSync(srcFile);
fileIo.closeSync(dstFile);
cloudStorage.bucket().uploadFile(GlobalContext.getContext(), {
localPath: cacheFile,
cloudPath: UI.uploadFileName,
mode: request.agent.Mode.BACKGROUND
})
.then((task: request.agent.Task) => {
task.on('progress', (progress) => {
hilog.info(0x0000, 'Storage', `on progress ${JSON.stringify(progress)}`);
});
task.on('completed', (progress) => {
hilog.info(0x0000, 'Storage', `on completed ${JSON.stringify(progress)}`);
fileIo.unlink(cacheFilePath).catch((err: BusinessError) => {
hilog.error(0x0000, 'Storage', `Failed to unlink file, code: ${err.code}, message: ${err.message}`);
});
hilog.info(0x0000, 'Storage', `delete cache file ${cacheFilePath}`);
});
task.on('failed', (progress) => {
hilog.info(0x0000, 'Storage', `on failed ${JSON.stringify(progress)}`);
fileIo.unlink(cacheFilePath).catch((err: BusinessError) => {
hilog.error(0x0000, 'Storage', `Failed to unlink file, code: ${err.code}, message: ${err.message}`);
});
hilog.info(0x0000, 'Storage', `delete cache file ${cacheFilePath}`);
});
task.start((err: BusinessError) => {
if (err) {
hilog.error(0x0000, 'Storage',
`Failed to start the uploadFileWithTask task, code: ${err.code}, message: ${err.message}`);
} else {
hilog.info(0x0000, 'Storage', `Succeeded in starting a uploadFileWithTask task.`);
}
});
})
.catch((error: BusinessError) => {
hilog.error(0x0000, 'Storage', `Failed to upLoadFile: code: ${error.code}, message: ${error.message}`);
});
} catch (e) {
hilog.info(0x0000, 'Storage', `uploadFile failed ${e.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0000, 'Storage', `Failed to pick photo view, code: ${err.code}, message: ${err.message}`);
})
After the upload is complete, you can sign in to AppGallery Connect, select a project, and go to the Cloud Storage page to view the file list.