Skip to content

Stream must be replayable to calculate a body hash #1473

@lon9man

Description

@lon9man

Describe the bug

Hello,
trying to upload file to S3 in Jetpack Compose.
Uri for file i got from PhotoPicker.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected behavior

file uploaded successfully

Current behavior

error: java.lang.IllegalArgumentException: Stream must be replayable to calculate a body hash

Steps to Reproduce

  1. click button "Open Gallery"
@Composable
fun GalleryScreen() {
    var selectedUris by remember {
        mutableStateOf<List<Uri>>(emptyList())
    }

    val multiplePhotoPickerLauncher =
        rememberLauncherForActivityResult(contract = ActivityResultContracts.PickMultipleVisualMedia(),
            onResult = { uris ->
                selectedUris = uris
            })

    fun onOpenGallery() {
        multiplePhotoPickerLauncher.launch(
            PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo)
        )
    }

    Row() {
          Button(onClick = ::onOpenGallery) {
              Text(text = "Open Gallery", color = MaterialTheme.colorScheme.onError)
          }
    }
}
  1. prepare additional parameters (bucket, key, metadata) and run fun upload
private suspend fun upload(bucket: String, key: String, metadata: Map<String, String>, uri: Uri) {
    val inputStream = context.contentResolver.openInputStream(uri)
        ?: throw IllegalArgumentException("Unable to open input stream for URI: ${config.uri}")

    try {
        inputStream.use { stream ->
            S3Client.use { s3 ->
                s3.putObject {
                    bucket = bucket
                    key = key
                    metadata = metadata
                    body = ByteStream.fromInputStream(stream)
                }
            }
        }
    } catch (e: Exception) {
        Log.d("TAG", "$e")
        throw e
    }
}

Possible Solution

No response

Context

No response

AWS SDK for Kotlin version

2.0.21

Platform (JVM/JS/Native)

Native

Operating system and version

Ubuntu 20.04.5 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.documentationThis is a problem with documentation.p2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions