Amazon web services provide us multiple services like Storage, Database, Application integration, etc.
Today let’s talk about the S3 service which is providing storage to upload your documents like global server storage. If you just want to use the S3 server directly(using IAM user) without cognito authentication then you just need to follow the few steps given below:
So the configuration of the project is done, Let’s move to the development part:
implementation 'com.amazonaws:aws-android-sdk-s3:2.16.4' implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.17.1@aar'){ transitive = true; }
<uses-permission-sdk-23 android:name="android.permission.INTERNET" /> <uses-permission-sdk-23 android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission-sdk-23 android:name="android.permission.ACCESS_WIFI_STATE" /> <application ...> <service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true"/> </application>
import android.content.Context import android.util.Log import com.amazonaws.auth.BasicAWSCredentials import com.amazonaws.mobileconnectors.s3.transferutility.* import com.amazonaws.regions.Region import com.amazonaws.regions.Regions import com.amazonaws.services.s3.AmazonS3Client import com.amazonaws.services.s3.model.CannedAccessControlList import com.urgidoctor.models.AwsCredentialsResponse import java.io.File class AWSS3Client( private val context: Context, private val awsFileUploadStatusListener: AWSFileUploadStatusListener ) { private val TAG = "AWSS3Client" fun uploadFile(file: File, awsCredentialsResponse: AwsCredentialsResponse, bucket:String) { startReadingAndUploading(file, awsCredentialsResponse,bucket) } private fun startReadingAndUploading( file: File, awsCredentialsResponse: AwsCredentialsResponse, bucket: String ) { val awsCredentials = BasicAWSCredentials( awsCredentialsResponse.awsAccessKey, awsCredentialsResponse.awsSecretKey ) val s3Client = AmazonS3Client(awsCredentials, Region.getRegion(Regions.US_EAST_1)) val transferUtility: TransferUtility = TransferUtility.builder() .context(context) .s3Client(s3Client) .build() TransferNetworkLossHandler.getInstance(context) val transferObserver: TransferObserver = transferUtility.upload( bucket, file.name, file, CannedAccessControlList.PublicReadWrite ) transferObserver.setTransferListener(object : TransferListener { override fun onStateChanged(id: Int, state: TransferState) { Log.e(TAG, "onStateChanged : $id $state") if (state === TransferState.COMPLETED) { val url = s3Client.getUrl(bucket, file.name) awsFileUploadStatusListener.fileUploadCompleted(url.toString()) Log.e(TAG, "onStateChanged url: $url") } else if (state === TransferState.CANCELED) { } } override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) { val percentDoneF = bytesCurrent.toFloat() / bytesTotal.toFloat() * 100 val percentDone = percentDoneF.toInt() Log.e(TAG, "onProgressChanged : $percentDone") } override fun onError(id: Int, exception: Exception) { Log.e(TAG, "onError : " + id + " " + exception.localizedMessage) awsFileUploadStatusListener.fileUploadError() } }) } interface AWSFileUploadStatusListener { fun fileUploadCompleted(url: String) fun fileUploadError() } }
Note: I have created a simple listener for the callback of complete and failure result so we can implement and get back the response in the same view
You can use this class like below:
//Simple AWS credential class to store keys class AwsCredentialsResponse { var awsAccessKey:String="your access key" var awsSecretKey:String="your secret key" } import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import com.urgidoctor.BuildConfig import com.urgidoctor.R import com.urgidoctor.databinding.ActivityCreateProfileStepsBinding import com.urgidoctor.models.AwsCredentialsResponse import com.urgidoctor.utils.* import java.io.File class CreateProfileStepsActivity : AppCompatActivity(), AWSS3Client.AWSFileUploadStatusListener{ private val fileObject: File lateinit var createProfileStepsBinding: ActivityCreateProfileStepsBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) createProfileStepsBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_profile_steps) uploadProfilePicOnAWS(fileObject) } private fun uploadProfilePicOnAWS(file: File) { AWSS3Client(this, this).uploadFile( file, AwsCredentialsResponse(), “Your bucket” ) } override fun fileUploadCompleted(url: String) { // you will get the se server file url here } override fun fileUploadError() { // handle failure case } }
Note: Please save your credentials somewhere when you are creating a new account or IAM user on AWS because you’ll not get a secret key in the future from the console.
In this article, you learned about file uploading on the AWS server. If you have any questions feel free to ask in the comments section.
How to Effectively Hire and Manage a Remote Team of Developers.
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, SafeZone Safety Systems
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and MindBowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Our CISO was extremely impressed by Mindbowser’s work. It is pretty rare to see this kind of clean security report so early in the company’s journey. Huge Thank you for the disciplined approach here.
Founder, TrestleIQ
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
The flexibility and capacity of the Mindbower staff has been impressive.
CEO, ProofPilot
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.
Founder, Courtyardly
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork