Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const buildUrl = ({bucketName, region}: IConfig): string => {
case "cn":
return `https://${bucketName}.s3.${region}.amazonaws.com.${countryCode}`;
default:
return `https://${bucketName}.s3-${region}.amazonaws.com`;
return `https://${bucketName}.s3.${region}.amazonaws.com`;
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/react-aws-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@ class ReactS3Client {
constructor(config: IConfig) {
this.config = config;
}

public async uploadDataFile(rawData: string, contentType: string, fileName: string): Promise<UploadResponse> {
const fd = new FormData();
const key: string = `${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`;
const url: string = GetUrl(this.config);
fd.append("key", key);
fd.append("acl", "public-read");
fd.append("Content-Type", contentType);
fd.append("x-amz-meta-uuid", "14365123651274");
fd.append("x-amz-server-side-encryption", "AES256");
fd.append(
"X-Amz-Credential",
`${this.config.accessKeyId}/${dateYMD}/${this.config.region}/s3/aws4_request`
);
fd.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
fd.append("X-Amz-Date", xAmzDate);
fd.append("x-amz-meta-tag", "");
fd.append("Policy", Policy.getPolicy(this.config));
fd.append(
"X-Amz-Signature",
Signature.getSignature(this.config, dateYMD, Policy.getPolicy(this.config))
);
var blob = new Blob([rawData], { type: contentType});
fd.append("file", blob);

const data = await fetch(url, { method: "post", body: fd });
if (!data.ok) return Promise.reject(data);
return Promise.resolve({
bucket: this.config.bucketName,
key: `${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`,
location: `${url}/${this.config.dirName ? this.config.dirName + "/" : ""}${fileName}`,
status: data.status
});
}

public async uploadFile(file: File, newFileName?: string): Promise<UploadResponse> {
throwError(this.config, file);

Expand Down Expand Up @@ -68,6 +103,7 @@ class ReactS3Client {
}
return `${newFileName || shortId.generate()}.${file.type.split("/")[1]}`;
}

}

export default ReactS3Client;