admin管理员组

文章数量:1410682

i get an object storage service from a pany and they use aws s3 structure.so you can use libraries designed for aws s3 to connect to there storage(libraries like boto3 for python and aws-sdk for javascript).i use aws-sdk.

i need to use an ip address to access the storage so i need to set endpoint to their ip address, and it works fine. problem is that this pany file storage system is a bit different than aws. while aws use bucket name as a subdomain, this pany use bucket name in the path of the file.

aws: bucket.amazon
their: <-ipaddress->/public/bucket/...

and its just fine when using boto3, but when i use aws-sdk and when i set bucket in params, aws-sdk set bucket as subdomin of ip address and it dont work any more.

since it work fine when using boto3, i thought it must work with aws-sdk as well. so im looking for a way to prevent aws-sdk to set bucket as subdomain and use it in a different way.

    const AWS = require("aws-sdk");
    const ep = new AWS.Endpoint("http://<ip address>");
    global.s3 = new AWS.S3({
      apiVersion: "2006-03-01",
      endpoint: ep,
    }); 
   

    const params = {
          Bucket: `${process.env.AWS_BUCKET}`,
          Prefix: `${_path}`,
        };

    const result = await s3.listObjects(params).promise();

result of the above code is this error: Inaccessible host: <bucket>.<ip address>. This service may not be available in the us-east-1' region.'

whick is currect because this address does not exists. but how can i fix this.

it works fine when im getting bucket list and bucket is not set.

i get an object storage service from a pany and they use aws s3 structure.so you can use libraries designed for aws s3 to connect to there storage(libraries like boto3 for python and aws-sdk for javascript).i use aws-sdk.

i need to use an ip address to access the storage so i need to set endpoint to their ip address, and it works fine. problem is that this pany file storage system is a bit different than aws. while aws use bucket name as a subdomain, this pany use bucket name in the path of the file.

aws: bucket.amazon.
their: <-ipaddress->/public/bucket/...

and its just fine when using boto3, but when i use aws-sdk and when i set bucket in params, aws-sdk set bucket as subdomin of ip address and it dont work any more.

since it work fine when using boto3, i thought it must work with aws-sdk as well. so im looking for a way to prevent aws-sdk to set bucket as subdomain and use it in a different way.

    const AWS = require("aws-sdk");
    const ep = new AWS.Endpoint("http://<ip address>");
    global.s3 = new AWS.S3({
      apiVersion: "2006-03-01",
      endpoint: ep,
    }); 
   

    const params = {
          Bucket: `${process.env.AWS_BUCKET}`,
          Prefix: `${_path}`,
        };

    const result = await s3.listObjects(params).promise();

result of the above code is this error: Inaccessible host: <bucket>.<ip address>. This service may not be available in the us-east-1' region.'

whick is currect because this address does not exists. but how can i fix this.

it works fine when im getting bucket list and bucket is not set.

Share Improve this question edited Nov 23, 2020 at 13:57 halo asked Nov 23, 2020 at 13:51 halohalo 10110 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

after three days looking for the answer, finally find the solution. aws s3 has two way of handling bucket.

  1. : use it in subdomain to connect to it

  2. : use it in the path of your object

in my case i should use the second way and the way to do it is to config aws-sdk and set s3ForcePathStyle to true. like this: AWS.config.s3ForcePathStyle = true;

and problem solved.

Based on the answer of @halo, I was able to figure out how to do this with the S3 Client to e.g connect to a s3 patible storage like OBS (open telekom cloud, Huawai cloud etc.) or to connect to e.g minio running locally.

import { S3Client } from '@aws-sdk/client-s3';
new S3Client({
    forcePathStyle: true, // needed for localstack
    endpoint: 'localhost:9000', //when using minio
    credentials: {
        secretAccessKey: 'hello',
        accessKeyId: 'supersecret',
    },
}),

本文标签: