Cloud Storage is scalable and maintenance-free. By integrating the Cloud Storage SDK, you can store high volumes of data such as image, audio, and video files generated by your users to the cloud or download these data from the cloud.
In this codelab, you will create an app that integrates Cloud Storage capabilities. Your app will allow users to query and delete files by calling APIs.
An iPhone or a simulator for testing
To integrate Cloud Storage of AppGallery Connect, you must complete the following preparations:
Before integrating the Cloud Storage SDK, you need to enable Cloud Storage in AppGallery Connect.


You need to integrate the Cloud Storage SDK into your Xcode project with CocoaPods.

cd project-directory
pod init
target 'AGC-Storage-2' do
pod 'AGConnectStorage'
pod 'AGConnectAuth'
end
pod install
The following figure shows the result after installation.
let resultLabel = UILabel(frame: CGRect(x: 60, y: 150, width: 300, height: 60))
override func viewDidLoad() {
super.viewDidLoad()
let signInButton = UIButton(frame: CGRect(x: 50, y: 80, width: 220, height: 50))
signInButton.backgroundColor = UIColor.blue
signInButton.setTitle("Sign In", for: .normal)
signInButton.addTarget(self, action: #selector(signInAnonymously), for: .touchUpInside)
self.view.addSubview(signInButton)
resultLabel.textColor = UIColor.darkGray
self.view.addSubview(resultLabel)
let uploadFileButton = UIButton(frame: CGRect(x: 50, y: 250, width: 220, height: 50))
uploadFileButton.backgroundColor = UIColor.blue
uploadFileButton.setTitle("Upload File", for: .normal)
uploadFileButton.addTarget(self, action: #selector(uploadFile), for: .touchUpInside)
self.view.addSubview(uploadFileButton)
let deleteFileButton = UIButton(frame: CGRect(x: 50, y: 320, width: 220, height: 50))
deleteFileButton.backgroundColor = UIColor.blue
deleteFileButton.setTitle("Delete File", for: .normal)
deleteFileButton.addTarget(self, action: #selector(deleteFile), for: .touchUpInside)
self.view.addSubview(deleteFileButton)
}

The sample code in this codelab uses the anonymous sign-in mode. Therefore, you need to enable anonymous authentication of Auth Service in AppGallery Connect. Otherwise, sign-in fails.

AGCAuth.instance().signInAnonymously().onSuccess { (result) in
self.resultLabel.text = "agc sign inanonymously success."
}.onFailure { (error) in
self.resultLabel.text = "agc sign inanonymously failed."
}
You can call related APIs of the Cloud Storage SDK to develop functions.
import AGConnectCore
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AGCInstance.startUp()
return true
}
var storage = AGCStorage.getInstanceForBucketName("codelab-test-wne6b")
@objc func uploadFile() {
let storageReference = storage.reference(withPath: "test1.jpg")
let dirPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first;
print(dirPath ?? "dirPath error")
let filePath = dirPath!.appending("/test1.jpg")
let task = storageReference.uploadFile(URL.init(fileURLWithPath: filePath))
task?.onSuccess(callback: { (result) in
self.resultLabel.text = "uploadFile success."
}).onFailure(callback: { (error) in
self.resultLabel.text = "uploadFile failed."
})
}
@objc func deleteFile() {
let storageReference = storage.reference(withPath: "test1.jpg")
let task = storageReference.deleteFile()
task.onSuccess(callback: { (result) in
self.resultLabel.text = "deleteFile success."
}).onFailure(callback: { (error) in
self.resultLabel.text = "deleteFile failed."
})
}


Tap Upload File to upload an image.
Go to Cloud Storage > Files to view the uploaded image.

Well done. You have successfully built an app that integrates Cloud Storage of AppGallery Connect and learned how to manage data on the Cloud Storage page in AppGallery Connect.