'Amazon S3 buckets 403 error on some phone models

The problem is getting buckets on the S3 cloud. The most interesting thing here is that everything works fine on most phones, but not on all models and mainly on Samsung models (there are also Xiaomi and other models). Unfortunately, I'm not very familiar with the service. Is it possible to track requests in the service itself and understand why it gives a 403 error?

Here is the exception I am getting:

Non-fatal Exception: retrofit2.HttpException: HTTP 403 Forbidden
       at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
       at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:129)
       at okhttp3.RealCall$AsyncCall.run(RealCall.kt:140)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:923)

Here is the method to sign and send keys:

class AwsSignInterceptor(val signer: OkHttpAwsV4Signer) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request().newBuilder()
                .addHeader("host", getHostHeader())
                .addHeader("x-amz-date", getXAmzDateheader())
                .addHeader("x-amz-content-sha256", "".sha256())
                .addHeader("Range", "")
                .build()
        val signedRequest = signer.sign(request, S3_OBJECT_STORAGE_ACCESS_KEY, S3_OBJECT_STORAGE_SECRET_KEY)
        FirebaseCrashlytics.getInstance().log(signedRequest.toString())
        return chain.proceed(signedRequest)
    } 

And here is the method where this exception is thrown:

            try {
                api.getAlbum("$albumIdInt").downloadToFileWithProgress(albumIdInt)
                        .collect { download ->
                            when (download) {
                                is Download.Progress -> {
                                    notifyWithProgress(download.percent)
                                }
                                is Download.Finished -> {
                                    val zip = download.file

                                    val result = mainInteractor.saveAlbum(albumIdInt.toString(), zip)
                                    infoInteractor.update(albumIdInt.toString())

                                    MainScope().launch {
                                        if (result) {
                                            notifyWithFinish()
                                        } else {
                                            notifyWithError()
                                            FirebaseCrashlytics.getInstance().log("Error during saving file")
                                        }
                                    }
                                }
                            }
                        }
            } catch (e: Exception) {
                MainScope().launch {
                    Log.e("http", "${e.message}")
                    notifyWithError()
                    FirebaseCrashlytics.getInstance().recordException(e)
                }
            }


Solution 1:[1]

Looks like you are trying to obtain Amazon S3 buckets from an Android Native app. If so, instead of writing your own Kotlin code, you should consider using the new AWS SDK for Kotlin.

You can use this SDK to easily invoke Amazon S3 from an Android app. Using this SDK and Android Studio, you can build a Native Android app that invokes Amazon S3 operations.

To demonstrate how to build an Android app using the AWS SDK for Kotlin that invokes AWS Services (this uses AWS SNS and Translation), follow this tutorial located in the AWS Kotlin DEV Guide.

Keep these requirements in mind as well:

  • Java 1.8 SDK Gradle 6.8 or higher Min
  • API version is 24
  • Requires core library desugaring in the Gradle build file
  • Source/target compat of 1.8

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1