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 });*/
setS3Credentials({
s3Region: "sos-ch-dk-2", s3Region: "sos-ch-dk-2",
s3Provider: "exo.io", s3Provider: "exo.io",
s3Key: "EXO15c0bf710e158e9b83270f0a", s3Key: "EXO15c0bf710e158e9b83270f0a",
s3Secret: "Dd5jYSiZtt_Zt5Ba5mDmaiLCdASUaKLfduSKY-SU-lg", s3Secret: "Dd5jYSiZtt_Zt5Ba5mDmaiLCdASUaKLfduSKY-SU-lg",
s3Bucket: "saliomameiringen", 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,45 +1,43 @@
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 bucket: string,
readonly region: string, readonly region: string,
readonly provider: string, readonly provider: string,
readonly key: string, readonly key: string,
readonly secret: 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}`;
}
public get(s3Path: string): Promise<Response> {
const method = "GET"; const method = "GET";
const auth = this.createAuthorizationHeader(method, s3Path, ""); const auth = this.createAuthorizationHeader(method, s3Path, "");
const url = this.url + "/" + s3Path const url = this.url + "/" + this.bucket + "/" + s3Path;
const headers = {"Host": this.host, "Authorization": auth}; const headers = { Host: this.host, Authorization: auth };
try try {
{ return fetch(url, { method: method, mode: "cors", headers: headers });
return fetch(url, {method: method, mode: "cors", headers: headers}) } catch {
} return Promise.reject();
catch
{
return Promise.reject()
} }
} }
private createAuthorizationHeader(method: string, private createAuthorizationHeader(
method: string,
s3Path: string, s3Path: string,
date: string) date: string
{ ) {
return createAuthorizationHeader return createAuthorizationHeader(
(
method, method,
this.bucket, this.bucket,
s3Path, s3Path,
@ -50,15 +48,16 @@ export class S3Access
} }
} }
function createAuthorizationHeader(method: string, function createAuthorizationHeader(
method: string,
bucket: string, bucket: string,
s3Path: string, s3Path: string,
date: string, date: string,
s3Key: string, s3Key: string,
s3Secret: string, s3Secret: string,
contentType: string = "", contentType: string = "",
md5Hash: string = "") md5Hash: string = ""
{ ) {
// StringToSign = HTTP-Verb + "\n" + // StringToSign = HTTP-Verb + "\n" +
// Content-MD5 + "\n" + // Content-MD5 + "\n" +
// Content-Type + "\n" + // Content-Type + "\n" +
@ -66,12 +65,14 @@ function createAuthorizationHeader(method: string,
// CanonicalizedAmzHeaders + // CanonicalizedAmzHeaders +
// CanonicalizedResource; // 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}`;
} }