求助:使用AWS与Mapbox API上传大体积.geojson文件失败
Hey there, let's work through your problems step by step to get that oversized GeoJSON file onto Mapbox. I'll break this down into two parts: fixing the AWS S3 access key error, and walking you through using the Mapbox JavaScript upload API (plus a simpler alternative you might prefer).
Part 1: Fixing the AWS S3 InvalidAccessKeyId Error
That error means the AWS access key you're using isn't recognized by AWS. Here's how to troubleshoot:
Double-check your key details
- Make sure you didn't mistype the Access Key ID (no extra spaces, correct case—AWS keys are case-sensitive).
- Verify the key belongs to your AWS account: Log into the AWS Console, go to IAM > Users > Your User > Security Credentials, and confirm the key you're using is listed as active (not deleted or expired).
Validate your AWS CLI setup
- If you're using the AWS CLI, check your credentials file:
- On Linux/macOS:
~/.aws/credentials - On Windows:
C:\Users\<YourUsername>\.aws\credentials
The file should look like this (replace placeholders with your actual keys):
[default] aws_access_key_id = YOUR_VALID_ACCESS_KEY aws_secret_access_key = YOUR_VALID_SECRET_KEY - On Linux/macOS:
- If you're passing keys directly in cURL commands, ensure they're correctly formatted (no typos, and you're using the right parameters for S3 authentication).
- If you're using the AWS CLI, check your credentials file:
Confirm your user has S3 permissions
- Your AWS user needs at least
s3:PutObjectands3:CreateMultipartUploadpermissions to upload files. For testing, you can temporarily attach theAmazonS3FullAccesspolicy to your user (restrict permissions later for security).
- Your AWS user needs at least
Match your bucket region
- Make sure the region specified in your AWS CLI/cURL command matches the region where your S3 bucket was created. For example, if your bucket is in
us-east-1, add--region us-east-1to your AWS CLI upload command.
- Make sure the region specified in your AWS CLI/cURL command matches the region where your S3 bucket was created. For example, if your bucket is in
Part 2: Using the Mapbox JavaScript Upload API (Simplified)
If the S3 route feels too cumbersome, there's an easier way—but let's first cover the JavaScript API workflow since that's what you asked about.
The core workflow for Mapbox's upload API is:
- Host your GeoJSON file in a publicly accessible location (like S3 with public read access, or a signed S3 URL).
- Use the Mapbox API to create an upload task that pulls the file from that location.
- Poll the API to check when the upload/processing is complete.
Step-by-Step JavaScript Example
First, grab a Mapbox access token with uploads:write permissions (create one in your Mapbox Console under Access Tokens). Then use this code (replace all uppercase placeholders):
// Replace with your actual Mapbox token (with uploads:write permission) const MAPBOX_TOKEN = 'YOUR_MAPBOX_ACCESS_TOKEN'; // Replace with the public URL of your GeoJSON file (e.g., S3 public URL) const FILE_URL = 'https://your-s3-bucket.s3.amazonaws.com/map.geojson'; // Replace with your tileset name (format: your-mapbox-username.tileset-name) const TILESET_NAME = 'your-username.large-geojson-tileset'; // Replace with a human-readable title for your tileset const TILESET_TITLE = 'My Large GeoJSON Dataset'; // 1. Create the upload task fetch(`https://api.mapbox.com/uploads/v1/your-mapbox-username?access_token=${MAPBOX_TOKEN}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url: FILE_URL, tileset: TILESET_NAME, name: TILESET_TITLE // Optional: Add simplification or other processing options here // simplify: 0.5 // Reduces file size by simplifying geometries }) }) .then(res => res.json()) .then(uploadData => { console.log('Upload task started:', uploadData); const uploadId = uploadData.id; // 2. Poll to check upload status (runs every 5 seconds) const statusInterval = setInterval(() => { fetch(`https://api.mapbox.com/uploads/v1/your-mapbox-username/${uploadId}?access_token=${MAPBOX_TOKEN}`) .then(res => res.json()) .then(statusData => { console.log('Current status:', statusData.status); if (statusData.status === 'completed') { clearInterval(statusInterval); console.log('Success! Your tileset is ready:', TILESET_NAME); } else if (statusData.status === 'failed') { clearInterval(statusInterval); console.error('Upload failed:', statusData.error); } }); }, 5000); }) .catch(err => console.error('Error starting upload:', err));
Even Simpler Alternative: Mapbox CLI
Skip the S3 and manual API calls entirely with the official Mapbox CLI tool. It handles uploading large files directly to Mapbox's infrastructure:
- Install the CLI via npm:
npm install -g @mapbox/mapbox-cli - Run the upload command (replace placeholders):
mapbox upload your-mapbox-username.your-tileset ./map.geojson --access-token YOUR_MAPBOX_TOKEN
The CLI will handle file splitting, uploading, and processing automatically—no need to mess with AWS at all.
内容的提问来源于stack exchange,提问作者Tharks

