fix style, fix bucket url

This commit is contained in:
Sina Blattmann 2023-07-13 10:17:53 +02:00
parent be18b291af
commit 8a50af825e
3 changed files with 74 additions and 76 deletions

View File

@ -29,17 +29,15 @@ const S3CredentialsContextProvider = ({
console.log("creds", s3Credentials); console.log("creds", s3Credentials);
const saveS3Credentials = (credentials: S3Credentials, id: string) => { const saveS3Credentials = (credentials: S3Credentials, id: string) => {
//const s3Bucket = id + "-3e5b3069-214a-43ee-8d85-57d72000c19d"; const s3Bucket = id + "-3e5b3069-214a-43ee-8d85-57d72000c19d";
/* const s3Bucket = "saliomameiringen"; setS3Credentials({ s3Bucket, ...credentials });
/* setS3Credentials({
setS3Credentials({ s3Bucket, ...credentials });*/ s3Region: "sos-ch-dk-2",
setS3Credentials({ s3Provider: "exo.io",
s3Region: "sos-ch-dk-2", s3Key: "EXO15c0bf710e158e9b83270f0a",
s3Provider: "exo.io", s3Secret: "Dd5jYSiZtt_Zt5Ba5mDmaiLCdASUaKLfduSKY-SU-lg",
s3Key: "EXO15c0bf710e158e9b83270f0a", s3Bucket: "saliomameiringen",
s3Secret: "Dd5jYSiZtt_Zt5Ba5mDmaiLCdASUaKLfduSKY-SU-lg", });*/
s3Bucket: "saliomameiringen",
});
}; };
const fetchData = (timestamp: UnixTime): Promise<FetchResult<DataRecord>> => { const fetchData = (timestamp: UnixTime): Promise<FetchResult<DataRecord>> => {

View File

@ -21,7 +21,6 @@ const InnovenergyTab = (props: any) => {
borderTopRightRadius: "0.3rem", borderTopRightRadius: "0.3rem",
padding: ".5rem 1rem", padding: ".5rem 1rem",
textDecoration: "none", textDecoration: "none",
transition: `color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out`,
"&.Mui-selected": { "&.Mui-selected": {
backgroundColor: "#eaecf1", backgroundColor: "#eaecf1",
color: colors.black, color: colors.black,

View File

@ -1,77 +1,78 @@
import {sha1Hmac} from "./Sha1"; import { sha1Hmac } from "./Sha1";
import {Utf8} from "./Utf8"; import { Utf8 } from "./Utf8";
import {toBase64} from "./UInt8Utils"; import { toBase64 } from "./UInt8Utils";
export class S3Access export class S3Access {
{ constructor(
constructor readonly bucket: string,
( readonly region: string,
readonly bucket: string, readonly provider: string,
readonly region: string, readonly key: string,
readonly provider: string, readonly secret: string
readonly key: string, ) {}
readonly secret: string
)
{}
get host() : string { return `${this.bucket}.${this.region}.${this.provider}` } get host(): string {
get url() : string { return `https://${this.host}` } return `${this.region}.${this.provider}`;
}
public get(s3Path : string): Promise<Response> get url(): string {
{ return `https://${this.host}`;
const method = "GET"; }
const auth = this.createAuthorizationHeader(method, s3Path, "");
const url = this.url + "/" + s3Path
const headers = {"Host": this.host, "Authorization": auth};
try public get(s3Path: string): Promise<Response> {
{ const method = "GET";
return fetch(url, {method: method, mode: "cors", headers: headers}) const auth = this.createAuthorizationHeader(method, s3Path, "");
} const url = this.url + "/" + this.bucket + "/" + s3Path;
catch const headers = { Host: this.host, Authorization: auth };
{
return Promise.reject() try {
} return fetch(url, { method: method, mode: "cors", headers: headers });
} catch {
return Promise.reject();
} }
}
private createAuthorizationHeader(method: string, private createAuthorizationHeader(
s3Path: string, method: string,
date: string) s3Path: string,
{ date: string
return createAuthorizationHeader ) {
( return createAuthorizationHeader(
method, method,
this.bucket, this.bucket,
s3Path, s3Path,
date, date,
this.key, this.key,
this.secret this.secret
); );
} }
} }
function createAuthorizationHeader(method: string, function createAuthorizationHeader(
bucket: string, method: string,
s3Path: string, bucket: string,
date: string, s3Path: string,
s3Key: string, date: string,
s3Secret: string, s3Key: string,
contentType: string = "", s3Secret: string,
md5Hash: string = "") contentType: string = "",
{ md5Hash: string = ""
// StringToSign = HTTP-Verb + "\n" + ) {
// Content-MD5 + "\n" + // StringToSign = HTTP-Verb + "\n" +
// Content-Type + "\n" + // Content-MD5 + "\n" +
// Date + "\n" + // Content-Type + "\n" +
// CanonicalizedAmzHeaders + // Date + "\n" +
// CanonicalizedResource; // CanonicalizedAmzHeaders +
// CanonicalizedResource;
const payload = Utf8.encode(`${method}\n${md5Hash}\n${contentType}\n${date}\n/${bucket}/${s3Path}`) const payload = Utf8.encode(
`${method}\n${md5Hash}\n${contentType}\n${date}\n/${bucket}/${s3Path}`
);
//console.log(`${method}\n${md5Hash}\n${contentType}\n${date}\n/${bucket}/${s3Path}`) //console.log(`${method}\n${md5Hash}\n${contentType}\n${date}\n/${bucket}/${s3Path}`)
const secret = Utf8.encode(s3Secret) const secret = Utf8.encode(s3Secret);
const signature = toBase64(sha1Hmac(payload, secret)); const signature = toBase64(sha1Hmac(payload, secret));
return `AWS ${s3Key}:${signature}` return `AWS ${s3Key}:${signature}`;
} }