使用PHP调用AWS Transcribe API遇400 Bad Request错误求助
Let’s walk through why you’re hitting that 400 Bad Request error when using PHP to call AWS Transcribe—even with full IAM permissions, there are several common pitfalls that trip people up. Here’s how to diagnose and fix them:
Common Causes & Fixes
Invalid S3 Media File URL
AWS Transcribe only accepts S3 URLs in thes3://bucket-name/path/to/your-file.mp3format. If you’re using an HTTP/HTTPS URL (likehttps://s3.amazonaws.com/...), that’s going to fail immediately. Double-check:- The bucket name is spelled correctly
- The file path matches exactly where your audio lives in S3
- The file actually exists in that location (no typos in the filename!)
Duplicate Transcription Job Name
AWS doesn’t let you reuse the same job name for any existing job—whether it’s active, completed, or failed. If you’ve already run a job namedJobName, you’ll get a 400 error right away. Try appending a unique identifier to the name, like a timestamp or UUID:JobName_202405201545orJobName_abc123.Mismatched or Invalid Media Format
Even though you specifiedmp3, make sure your audio file is actually a valid MP3. If it’s corrupted, mislabeled (e.g., a WAV file renamed to .mp3), or uses an unsupported codec, Transcribe will reject it. You can verify the file format with tools likeffmpegor check its metadata to confirm.S3 Bucket Policy Blocking Transcribe
Wait—even if your IAM user has full permissions, the S3 bucket itself might have a policy that stops Transcribe from accessing the file. Transcribe needs explicit permission to read the media object. Add a statement to your bucket policy that allowss3:GetObjectfor the Transcribe service:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "transcribe.amazonaws.com" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::your-bucket-name/*" } ] }Region Mismatch
Make sure your PHP Transcribe client is initialized with the same AWS region as your S3 bucket. If your bucket is inus-east-1but your client is set toeu-west-1, that’s a guaranteed 400 error. Double-check the region parameter when creating your client instance.
Quick Debug Hack
Add error handling to your PHP code to get precise error details from AWS—this will save you tons of guessing. Wrap your API call in a try-catch block to capture the exact error code and message:
try { $response = $this->client->startTranscriptionJob([ 'LanguageCode' => 'en-US', 'Media' => [ 'MediaFileUrl' => 's3://your-bucket/path/to/audio.mp3' ], 'MediaFormat' => 'mp3', 'TranscriptionJobName' => 'UniqueJobNameHere' ]); } catch (\Aws\TranscribeService\Exception\TranscribeServiceException $e) { echo "Error Message: " . $e->getMessage() . "\n"; echo "AWS Error Code: " . $e->getAwsErrorCode() . "\n"; }
The error code (like InvalidJobName or AccessDenied) will point you straight to the root issue.
内容的提问来源于stack exchange,提问作者EchoAro

