1+ package thecloudhub.com.digitaloceanspacesexample
2+
3+ import android.content.Context
4+ import android.graphics.Bitmap
5+ import android.graphics.BitmapFactory
6+ import android.util.Log
7+ import com.amazonaws.auth.BasicAWSCredentials
8+ import com.amazonaws.internal.StaticCredentialsProvider
9+ import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener
10+ import com.amazonaws.mobileconnectors.s3.transferutility.TransferState
11+ import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility
12+ import com.amazonaws.regions.Region
13+ import com.amazonaws.services.s3.AmazonS3Client
14+ import java.io.ByteArrayOutputStream
15+ import java.io.File
16+ import java.io.FileOutputStream
17+ import java.lang.Exception
18+ import java.util.*
19+
20+ interface SpaceRegionRepresentable {
21+ fun endpoint (): String
22+ }
23+
24+ /* *
25+ * Represents a region in which a Digital Ocean Space can be created
26+ */
27+ enum class SpaceRegion : SpaceRegionRepresentable {
28+ SFO {
29+ override fun endpoint (): String {
30+ return " https://sfo2.digitaloceanspaces.com"
31+ }
32+ }, AMS {
33+ override fun endpoint (): String {
34+ return " https://ams3.digitaloceanspaces.com"
35+ }
36+ }, SGP {
37+ override fun endpoint (): String {
38+ return " https://sgp1.digitaloceanspaces.com"
39+ }
40+ }
41+ }
42+
43+ class SpacesFileRepository (context : Context ) {
44+ private val accesskey = " YOUR_KEY_HERE"
45+ private val secretkey = " YOUR_SECRET_HERE"
46+ private val spacename = " YOUR_BUCKET_NAME_HERE"
47+ private val spaceregion = SpaceRegion .SFO
48+
49+ private val filename = " example_image"
50+ private val filetype = " jpg"
51+
52+ private var transferUtility: TransferUtility
53+ private var appContext: Context
54+
55+ init {
56+ val credentials = StaticCredentialsProvider (BasicAWSCredentials (accesskey, secretkey))
57+ val client = AmazonS3Client (credentials, Region .getRegion(" us-east-1" ))
58+ client.endpoint = spaceregion.endpoint()
59+
60+ transferUtility = TransferUtility .builder().s3Client(client).context(context).build()
61+ appContext = context
62+ }
63+
64+ /* *
65+ * Converts a APK resource to a file for uploading with the S3 SDK
66+ */
67+ private fun convertResourceToFile (): File {
68+ val exampleIdentifier = appContext.resources.getIdentifier(filename, " drawable" , appContext.packageName)
69+ val exampleBitmap = BitmapFactory .decodeResource(appContext.resources, exampleIdentifier)
70+
71+ val exampleFile = File (appContext.filesDir, Date ().toString())
72+ exampleFile.createNewFile()
73+
74+ val outputStream = ByteArrayOutputStream ()
75+ exampleBitmap.compress(Bitmap .CompressFormat .JPEG , 100 , outputStream)
76+ val exampleBitmapData = outputStream.toByteArray()
77+
78+ val fileOutputStream = FileOutputStream (exampleFile)
79+ fileOutputStream.write(exampleBitmapData)
80+ fileOutputStream.flush()
81+ fileOutputStream.close()
82+
83+ return exampleFile
84+ }
85+
86+ /* *
87+ * Uploads the example file to a DO Space
88+ */
89+ fun uploadExampleFile (){
90+ // Starts the upload of our file
91+ var listener = transferUtility.upload(spacename, " $filename .$filetype " , convertResourceToFile())
92+
93+ // Listens to the file upload progress, or any errors that might occur
94+ listener.setTransferListener(object : TransferListener {
95+ override fun onError (id : Int , ex : Exception ? ) {
96+ Log .e(" S3 Upload" , ex.toString())
97+ }
98+
99+ override fun onProgressChanged (id : Int , bytesCurrent : Long , bytesTotal : Long ) {
100+ Log .i(" S3 Upload" , " Progress ${((bytesCurrent/ bytesTotal)* 100 )} " )
101+ }
102+
103+ override fun onStateChanged (id : Int , state : TransferState ? ) {
104+ if (state == TransferState .COMPLETED ){
105+ Log .i(" S3 Upload" , " Completed" )
106+ }
107+ }
108+ })
109+ }
110+
111+ /* *
112+ * Downloads example file from a DO Space
113+ */
114+ fun downloadExampleFile (callback : (File ? , Exception ? ) -> Unit ) {
115+ // Create a local File object to save the remote file to
116+ val file = File (" ${appContext.cacheDir} /$filename .$filetype " )
117+
118+ // Download the file from DO Space
119+ var listener = transferUtility.download(spacename, " $filename .$filetype " , file)
120+
121+ // Listen to the progress of the download, and call the callback when the download is complete
122+ listener.setTransferListener(object : TransferListener {
123+ override fun onProgressChanged (id : Int , bytesCurrent : Long , bytesTotal : Long ) {
124+ Log .i(" S3 Download" , " Progress ${((bytesCurrent/ bytesTotal)* 100 )} " )
125+ }
126+
127+ override fun onStateChanged (id : Int , state : TransferState ? ) {
128+ if (state == TransferState .COMPLETED ){
129+ Log .i(" S3 Download" , " Completed" )
130+ callback(file, null )
131+ }
132+ }
133+
134+ override fun onError (id : Int , ex : Exception ? ) {
135+ Log .e(" S3 Download" , ex.toString())
136+ callback(null , ex)
137+ }
138+ })
139+ }
140+ }
0 commit comments