Use uma barra de progresso para indicar o andamento do upload ou download de um objeto. O exemplo a seguir usa a operação PutObject para demonstrar como exibir a barra de progresso durante o upload de um objeto.
Observações
-
Antes de executar o código de exemplo deste tópico, crie uma instância OSSClient por meio de métodos como nome de domínio personalizado ou Security Token Service (STS). Para mais informações, consulte Inicialização.
NotaA região do bucket é determinada pela região do endpoint especificado na inicialização.
Para fazer upload de um arquivo, você deve ter a permissão
oss:PutObject. Para mais informações, consulte Conceder políticas de acesso personalizadas a um usuário RAM.
Exemplos
O código de exemplo a seguir mostra como exibir a barra de progresso ao fazer upload de um arquivo local para um bucket:
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// Specify the name of the bucket. Example: examplebucket.
put.bucketName = @"examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
put.objectKey = @"exampledir/exampleobject.txt";
// Specify the full path of the local file that you want to upload.
// By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs.
put.uploadingFileURL = [NSURL fileURLWithPath:@"filePath"];
// Configure the progress callback function to display the progress bar.
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// Specify the number of bytes that are being uploaded, the total number of bytes that are uploaded, and the total number of bytes that you want to upload.
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
NSLog(@"upload object success!");
} else {
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
// Implement synchronous blocking to wait for the task to complete.
// [putTask waitUntilFinished];
Referências
Para obter o código de exemplo completo sobre barras de progresso no upload de objetos, visite o GitHub.
Para mais informações sobre como inicializar uma instância OSSClient, consulte Inicialização.