web-dev-qa-db-fra.com

AWS CLI obtenir l'URL de téléchargement S3 pour le compartiment privé à partir d'AWS CLI

Je pourrais télécharger un fichier vers un privé compartiment S3 avec succès en utilisant la commande suivante:

aws s3 cp "myfile.txt" "s3://myfolder/myfile.txt" --region=us-east-1 --output=json

Je voudrais émettre une commande AWS CLI pour me renvoyer un téléchargement d'URL temporaire pour monfichier.txt et quelqu'un sait-il comment le faire?

J'ai recherché sur Google et je dois signer pour obtenir une URL temporaire telle que: http://docs.aws.Amazon.com/AmazonS3/latest/dev/RESTAuthentication.html

24
Nam Nguyen

J'ai fait une recherche sur Google et j'ai décidé d'écrire ce script pour m'aider à générer une URL-singed pour S3.

https://github.com/gdbtek/aws-tools

12
Nam Nguyen

aws cli prend désormais en charge la commande presign. Tu peux courir

$ aws s3 presign s3://test-bucket/test-file.txt
https://test-bucket/test-file.txt?Expires=1499152189&Signature=some-sha

Cela générera une URL que vous pourrez partager avec n'importe qui pour télécharger ce fichier en 3600 secondes.

Vous pouvez modifier la période avec --expires-in

$ aws s3 presign s3://test-bucket/test-file.txt --expires-in 600 

L'URL générée expirera dans 10 minutes.

Vous pouvez en savoir plus sur presign dans aws cli docs .

19
ChillarAnand

Vous pouvez utiliser le format URL suivant:

https://<bucket-name>.s3.amazonaws.com/<object or key name>

ou à l'ancienne:

https://s3.amazonaws.com/<bucket-name>/<object or key name>

Pour le rendre accessible, vous devez autoriser l'accès public à votre objet ou attacher une stratégie de compartiment appropriée.

Par exemple, la stratégie de compartiment suivante montre l'accès public au compartiment zzzyyy objet 'yyyeee'

$ aws s3 get-object-acl --bucket zzzyyy --key yyyeee
{
    "Owner": {
        "DisplayName": "owner",
        "ID": "Some hash of owner"
    },
    "Grants": [
        {
            "Grantee": {
                "DisplayName": "owner",
                "ID": "Some hash of owner"
            },
            "Permission": "READ"
        },
        {
            "Grantee": {
                "DisplayName": "owner",
                "ID": "Some hash of owner"
            },
            "Permission": "WRITE"
        },
        {
            "Grantee": {
                "DisplayName": "owner",
                "ID": "Some hash of owner"
            },
        "Permission": "READ_ACP"
        },
        {
            "Grantee": {
                "DisplayName": "owner",
                "ID": "Some hash of owner"
            },
            "Permission": "WRITE_ACP"
        },
        {
            "Grantee": {
                "URI": "http://acs.amazonaws.com/groups/global/AllUsers"
            },
            "Permission": "READ"
        },
        {
            "Grantee": {
                "URI": "http://acs.amazonaws.com/groups/global/AllUsers"
            },
            "Permission": "READ_ACP"
        }
    ]
}

Vous pouvez voir des exemples de politiques de compartiment ici:

http://docs.aws.Amazon.com/AmazonS3/latest/dev/AccessPolicyLanguage_UseCases_s3_a.html

Vous pouvez également utiliser la console S3 comme indiqué ici:

S3 Console

10
Rico

La commande est fausse; au lieu de:

$ aws s3 get-object-acl --bucket zzzyyy --key yyyeee

Ça devrait être:

$ aws s3api get-object-acl --bucket zzzyyy --key yyyeee
2
TimT

Alternativement, vous pouvez jeter un œil à https://github.com/minio/mc et nous faire part de vos commentaires. Nous avons toujours voulu avoir une interface dropbox CLI, bien sûr pas en GUI mais scriptable et automatisée. Nous avons donc fini par implémenter une commande pratique pour mc pour répondre à deux cas d'utilisation.

  • Pour partager des URL pour télécharger des fichiers (Get présélectionnée)
  • Pour partager curl formulaire pour télécharger des fichiers (Presigned POST Policy)

Les exemples suivants traitent des deux cas d'utilisation.

$ mc share
NAME:
  mc share - Download and upload documents.

USAGE:
  mc share command [arguments...]

COMMANDS:
  download  Generate URL to download documents.
  upload    Generate ‘curl’ command to upload files.
  list      List the shared URLs

Ces commandes peuvent être utilisées au sens ordinaire ou également à partir de avec dans un script. À des fins de script, mc fournit --json sortie pour toutes les commandes.

Exemple pour mc share download

$ mc share download https://s3.amazonaws.com/ferenginar/sur_2013-08-05T05_48_49-07_00.mp3
https://s3.amazonaws.com/ferenginar/sur_2013-08-05T05_48_49-07_00.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJVA5BMMU2RHO6IOQ%2F20151109%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20151109T060251Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=Host&X-Amz-Signature=3c69928bee38b7b465547f9fa97146ff606480bbead8042c0351840c578a2ceb

$ mc --json share download https://s3.amazonaws.com/ferenginar/sur_2013-08-05T05_48_49-07_00.mp3 | jq .

{
  "expiry": {
    "days": 7
  },
  "downloadUrl": "https://s3.amazonaws.com/ferenginar/sur_2013-08-05T05_48_49-07_00.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJVA5BMMU2RHO6IOQ%2F20151109%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20151109T060618Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=Host&X-Amz-Signature=48f7a521396ac8e36443cc86ce16c2619f9fa81371aad9a309375c933ea48d99",
  "keyName": "https://s3.amazonaws.com/ferenginar/sur_2013-08-05T05_48_49-07_00.mp3"
}

Exemple pour mc share upload

$ mc share upload --expire=2h --content-type=image/png https://s3.amazonaws.com/backup/2007-Mar-2/... 
curl https://s3.amazonaws.com/backup -F x-amz-credential=AKIAJVA5BMMU2RHO6IOQ/20151109/us-east-1/s3/aws4_request -F x-amz-date=20151109T061202Z -F x-amz-signature=61c234dd17fc9e5888417ac4546b37d61755e551674a9617f33f5232ec53c8b6 -F Content-Type=image/png -F bucket=backup -F policy=eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0wOVQwODoxMjowMi40MDZaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkQ29udGVudC1UeXBlIiwiaW1hZ2UvcG5nIl0sWyJlcSIsIiRidWNrZXQiLCJiYWNrdXAiXSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIjIwMDctTWFyLTIvIl0sWyJlcSIsIiR4LWFtei1kYXRlIiwiMjAxNTExMDlUMDYxMjAyWiJdLFsiZXEiLCIkeC1hbXotYWxnb3JpdGhtIiwiQVdTNC1ITUFDLVNIQTI1NiJdLFsiZXEiLCIkeC1hbXotY3JlZGVudGlhbCIsIkFLSUFKVkE1Qk1NVTJSSE82SU9RLzIwMTUxMTA5L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QiXV19 -F x-amz-algorithm=AWS4-HMAC-SHA256 -F key=2007-Mar-2/<FILE> -F file=@<FILE>

$ mc --json share upload --expire=2h --content-type=image/png https://s3.amazonaws.com/backup/2007-Mar-2/... | jq .
{
  "expiry": {
    "hours": 2
  },
  "uploadCommand": "curl https://s3.amazonaws.com/backup -F x-amz-signature=4b349a15e76ab403424ec6b1e289336f67c02c427ccc98522d6f654eee5b7de6 -F Content-Type=image/png -F bucket=backup -F policy=eyJleHBpcmF0aW9uIjoiMjAxNS0xMS0wOVQwODoxMzoyMS4zODFaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkQ29udGVudC1UeXBlIiwiaW1hZ2UvcG5nIl0sWyJlcSIsIiRidWNrZXQiLCJiYWNrdXAiXSxbInN0YXJ0cy13aXRoIiwiJGtleSIsIjIwMDctTWFyLTIvIl0sWyJlcSIsIiR4LWFtei1kYXRlIiwiMjAxNTExMDlUMDYxMzIxWiJdLFsiZXEiLCIkeC1hbXotYWxnb3JpdGhtIiwiQVdTNC1ITUFDLVNIQTI1NiJdLFsiZXEiLCIkeC1hbXotY3JlZGVudGlhbCIsIkFLSUFKVkE1Qk1NVTJSSE82SU9RLzIwMTUxMTA5L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QiXV19 -F x-amz-algorithm=AWS4-HMAC-SHA256 -F x-amz-credential=AKIAJVA5BMMU2RHO6IOQ/20151109/us-east-1/s3/aws4_request -F x-amz-date=20151109T061321Z -F key=2007-Mar-2/<FILE> -F file=@<FILE> ",
  "keyName": "https://s3.amazonaws.com/backup/2007-Mar-2/..."
}

Dans l'attente de certains commentaires. Merci

2
Harshavardhana

Je ne pense pas qu'il y ait une commande cli pré-construite\sub pour obtenir l'url du compartiment privé, pour la pré-signature, vous pouvez vous référer presign . Si vous souhaitez une URL publique dans un compartiment privé, c'est-à-dire que l'objet lu est public et non pas bucket. Pour l'url publique aws, vous pouvez utiliser quelque chose - j'ai écrit ceci en PowerShell, vous pouvez mettre ceci $profile

Function getAwsUrl{
[CmdletBinding(DefaultParameterSetName="S3Path")]
    param(
    [parameter (ParameterSetName="S3Path",mandatory=$true)]$s3path,
    [parameter (ParameterSetName="BucketKey")]$bucket,
    [parameter (ParameterSetName="BucketKey",mandatory=$true)]$key 
    )
if($s3path)
{
$splits = $s3path.Split('//')
$bucket =$splits[2]
$key = $splits[3]
}
else 
{
#user input bucket and key
$bucket =$bucket
$key = $key
}

#using standard aws region "us-west-2"
Write-Output "https://$bucket.s3.us-west-2.amazonaws.com/$key"
}

Exemple d'utilisation : assurez-vous de passer --acl en lecture publique

aws s3 cp C:\temp\dumy.txt s3://fpd-uploads/ --acl public-read
getAwsUrl -s3path s3://fpd-uploads/dumy.txt

get-help getAwsUrl

NAME
    getAwsUrl

SYNTAX
    getAwsUrl -s3path <Object>  [<CommonParameters>]

    getAwsUrl -key <Object> [-bucket <Object>]  [<CommonParameters>]


ALIASES
    None


REMARKS
    None
0
Madhukar Moogala

Sachez que c'est 8 mois de retard, mais je viens de découvrir que vous pouvez utiliser Eclipse avec AWS Toolkit for Eclipse ( http://aws.Amazon.com/Eclipse/ ) pour générer des URL pré-signées avec un clic droit sur le fichier cible, vous permet même de définir la date d'expiration .... C'est assez lisse.

0
TrackZero