You can upload a media file to ApsaraVideo VOD. The media file can be a local file or an online file. On iOS devices, the upload instance VODUploadClient is used to upload files. This topic describes how to use the upload SDK for iOS to upload a file.
Prerequisites
The upload SDK for iOS is integrated. For more information, see Integrate the upload SDK for iOS.
Upload processes
Client upload SDKs encapsulate the logic of uploading files to OSS buckets. When you upload medial files from clients, the files are directly uploaded to OSS buckets allocated by ApsaraVideo VOD without forwarding by servers. Therefore, the clients must be authenticated. You must deploy an authorization service on your application server to obtain upload URLs and credentials. Client upload SDKs support authorization based on upload credentials and authorization based on STS tokens. We recommend that you use an upload credential. For more information, see Comparison between credentials and STS.
Process of uploading a media file by using an upload URL and an upload credential
The following figure shows the complete process of uploading a media file by using an upload URL and an upload credential. In this example, ApsaraVideo VOD SDK is integrated to obtain the upload URL and credential.
A user deploys an authorization service on an application server, such as ApsaraVideo VOD SDK, to obtain upload URLs and credentials.
A client sends a request to the application server to request an upload URL and an upload credential.
The application server sends a request to ApsaraVideo VOD to request the upload URL and credential.
ApsaraVideo VOD returns the upload URL and credential.
NoteApsaraVideo VOD also generates media IDs, which can be used in media lifecycle management and media processing.
For a video, the media ID is returned in the
VideoId
parameter.For an image, the media ID is returned in the
ImageId
parameter.For an auxiliary media asset, the media ID is returned in the
MediaId
parameter.Properly keep the returned media IDs, which are required in subsequent tasks, such as media asset management, audio and video playback, and media processing.
The application server returns the upload URL and credential to the client.
NoteThe application server does not need to perform Base64 decoding on the upload URL or credential.
The client uses the upload URL and credential to initialize an upload instance.
The client constructs upload parameters to send an upload request.
OSS returns the upload result.
NoteYou can also set callbacks in advance to receive notifications about upload events.
Process of uploading a media file by using an STS token
If you use authorization based on the STS token, you must create a RAM role and grant the RAM role permissions to access ApsaraVideo VOD. For more information, see Use STS to upload videos.
The following figure shows the complete process of uploading a media file by using an STS token.
A user deploys an authorization service on an application server, such as ApsaraVideo VOD SDK, to obtain temporary STS tokens.
A client sends a request to the application server to request an STS token.
The application server sends a request to STS to request the STS token.
STS returns the token.
The application server returns the STS token to the client.
The client uses the STS token to initialize an upload instance.
The client constructs upload parameters to send an upload request.
OSS returns the upload result.
NoteYou can also set callbacks in advance to receive notifications about upload events.
Upload a file
To upload a file on iOS, perform the following steps:
Obtain an upload URL and an upload credential or a Security Token Service (STS) token for upload authorization.
Two authorization methods are provided for the upload SDK for iOS. We recommend that you use an upload credential for authorization.
- For more information about how to obtain the upload URL and credential, see Obtain upload URLs and credentials.
- For more information about how to obtain an STS token, see Obtain an STS token.
Result
Use the obtained upload URL and credential or the obtained STS token to initialize the upload instance.
Initialize the upload instance by using the upload URL and credential or the STS token.
You need to configure the initialization callback before you initialize the upload instance.
Declare the
VODUploadClient
instance, which cannot be a local variable.Initialize the upload instance. Use the upload URL and credential or the STS token to initialize the upload instance based on your business requirements.
(Recommended) Initialize the upload instance by using the upload URL and credential
NoteIf you use the upload URL and credential, call the
init
method to initialize the upload instance.You do not need to specify the obtained upload URL and credential during initialization. Instead, specify them in the SDK by calling the
setUploadAuthAndAddress: uploadAuth:uploadAddress:
method in theOnUploadStartedListener
callback that is fired when the upload starts.If the upload credential expires, the
OnUploadTokenExpiredListener
callback is fired. To resume the upload by using a new upload credential, call theresumeWithAuth
method.
Initialize the upload instance by using the STS token
NoteIf you use the STS token, call the
init
method to initialize the upload instance and thesetKeyId:accessKeySecret:secretToken:expireTime:listener:
method to specify the STS token.When the STS token expires, the
OnUploadTokenExpiredListener
callback is fired. To resume the upload by using a new STS token, call theresumeWithToken: accessKeySecret: secretToken: expireTime
method.
Configure callbacks to receive key messages during the upload.
Set the
VODUploadListener
object, which belongs to the class of callbacks for upload statuses. You must set the following callbacks:Construct upload parameters based on the type of the file to be uploaded. You can upload audio, video, or image files.
NoteThe upload parameters for images are slightly different from those for audio and video files. You cannot upload auxiliary media assets by using client upload SDKs.
Upload parameters for audio and video files
Construct an upload request function to add an audio or video file to the upload list.
Structure of the VodInfo object
After you add a file to upload, the SDK encapsulates the file in the
UploadFileInfo
object that has the following structure:NoteIf you want to upload a video from an album, set the filePath parameter to the absolute path of the video that is selected by using the selector.
Upload parameters for image files
Start the upload.
The
start
method is called to start the upload.[self.uploader start];
When this method is called, the
OnUploadStartedListener
callback is fired. If you use an upload URL and an upload credential to upload a file, specify the upload URL and credential in this callback, as shown in the following code:[weakSelf.uploader setUploadAuthAndAddress:fileInfo uploadAuth:weakSelf.uploadAuth uploadAddress:weakSelf.uploadAddress];
When the upload starts, the
OnUploadProgressListener
callback is fired to synchronize the upload progress.The callback parameters include
uploadedSize
andtotalSize
. The uploadedSize parameter indicates the size of the uploaded parts, and the totalSize parameter indicates the total size of the file that you upload.After the file is uploaded, the
OnUploadFinishedListener
callback is returned. In the callback message, theUploadFileInfo
parameter indicates the file information and theVodUploadResult
parameter indicates the upload result.The
VodUploadResult
object contains the following properties:@property (nonatomic, copy) NSString* videoId; @property (nonatomic, copy) NSString* imageUrl;
NoteA value is returned for the
videoId
parameter only if a video is uploaded by using the STS token. A value is returned for theimageUrl
parameter only if an image is uploaded by using the STS token. If you use the upload URL and credential to upload a file, thevideoId
andimageUrl
parameters are not returned. You can obtain the values of the parameters when you obtain the upload URL and credential.
Result
After a video is uploaded, the videoId parameter is returned. The value of the videoId parameter indicates the video ID. Then, you can obtain the streaming URL to play the video. For more information, see Obtain playback URLs to play videos.
After an image is uploaded, the imageUrl parameter is returned. The value of the imageUrl parameter indicates the image URL. If URL signing is enabled, the image URL expires after a specific period of time. For more information, see Configure URL signing.
Queue management
The upload instance VODUploadClient
allows you to add multiple files to be uploaded in sequence. You can use the following methods to manage the upload list:
The VODUploadClient
object allows you to upload multiple files at a time. However, if you use upload URLs and credentials to upload media files, you must separately set the configuration for each file. The code for uploading multiple files at a time is complex. Therefore, we recommend that you add a single file to the upload list at a time.
Remove a file from the upload list. If the file to be removed is being uploaded, the system cancels the upload and automatically starts to upload the next file in the list.
- (BOOL)deleteFile:(int) index;
Clear the upload list. If the upload list to be cleared contains a file that is being uploaded, the system cancels the upload.
- (BOOL)clearFiles;
Query the upload list.
- (NSMutableArray<UploadFileInfo *> *)listFiles;
Mark a file in the upload list as canceled without removing it from the upload list. If the file to be canceled is being uploaded, the system cancels the upload and automatically starts to upload the next file in the list.
- (BOOL)cancelFile:(int)index;
Resume a canceled file and automatically start to upload the file.
- (BOOL)resumeFile:(int)index;
Upload management
Stop the upload. If a file is being uploaded, the system cancels the upload.
- (BOOL)stop;
NoteIf you want to resume the upload after you stop the upload, call the
resumeFile
method to resume the file to be uploaded. Alternatively, clear the upload list and add the file again to upload it.Pause the upload.
- (BOOL)pause;
Resume the upload.
- (BOOL)resume;
Handle callbacks
Upload failure
The
OnUploadFailedListener
callback is invoked if the upload fails. You can view the cause of the failure based on thecode
andmessage
callback parameters. In addition, a prompt is displayed on the web page. For more information about error codes, see Error codes and Handle exceptions.Credential or token expiration
The
OnUploadTokenExpiredListener
callback is invoked if the upload credential or STS token expires. You can send a request to the AppServer to obtain a new upload credential or STS token, and call the following method to resume the upload:Specify a new upload credential
- (BOOL)resumeWithAuth:(NSString *)uploadAuth;
Specify a new STS token
- (BOOL)resumeWithToken:(NSString *)accessKeyId accessKeySecret:(NSString *)accessKeySecret secretToken:(NSString *)secretToken expireTime:(NSString *)expireTime;
Upload timeout
If the upload times out, the
OnUploadRertyListener
callback is invoked and the system automatically retries to upload the file. You can receive a prompt on the web page or call thestop
method to stop the upload. In addition, you can set themaxRetryCount
parameter to specify the maximum number of retries. If the retry result indicates that the upload can be resumed, theOnUploadRertyResumeListener
callback is invoked and the upload is resumed.
Advanced settings
The VODUploadClient
instance supports the following advanced settings: transcoding, retries upon a timeout, cache path, resumable upload, multipart upload, and service region. Sample code:
Transcoding
/**
Specify whether to transcode a video when it is uploaded to ApsaraVideo VOD. Default value: YES. For more information about video transcoding formats, see Audio and video transcoding.
*/
@property (nonatomic, assign) BOOL transcode;
Retries upon a timeout
/**
Specify the maximum number of retries allowed upon a timeout. Default value: INT_MAX.
*/
@property (nonatomic, assign) uint32_t maxRetryCount;
/**
Specify the timeout period.
*/
@property (nonatomic, assign) NSTimeInterval timeoutIntervalForRequest;
Cache path
/**
Specify the path in which cached data is stored.
*/
@property (nonatomic, copy) NSString * recordDirectoryPath;
Resumable upload
/**
Specify whether to record the upload progress for a resumable upload. Default value: YES.
*/
@property (nonatomic, assign) BOOL recordUploadProgress;
Multipart upload
/**
Specify the size of each part in multipart upload. Default value: 1024 × 1024.
*/
@property (nonatomic, assign) NSInteger uploadPartSize;
Service region
/**
Specify the region in which ApsaraVideo VOD is activated. Default value: cn-shanghai.
*/
@property (nonatomic, copy) NSString *region;