Use uma barra de progresso para indicar o andamento do upload ou download de um objeto. Neste exemplo, a operação PutObject ilustra 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 do OSSClient por meio de métodos como o uso de um nome de domínio personalizado ou do Security Token Service (STS). Para obter mais informações, consulte Initialization.
NotaA região do bucket é definida pela região do endpoint especificado na inicialização.
Para fazer upload de um arquivo, você precisa ter a permissão
oss:PutObject. Para obter mais informações, consulte Grant custom access policies to a RAM user.
Exemplos
O código de exemplo a seguir demonstra 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 acessar o código de exemplo completo da barra de progresso no upload de objetos, visite GitHub.
Para obter mais informações sobre como inicializar uma instância do OSSClient, consulte Initialization.