【问题标题】:How to fix boto3 aws botocore.exceptions.NoCredentialsError:如何修复 boto3 aws botocore.exceptions.NoCredentialsError:
【发布时间】:2021-11-21 11:20:51
【问题描述】:
from django.core.management.base import BaseCommand, CommandError
from crocolinks.models import CrocoLink
from datetime import datetime
import os
import shutil
import boto3
import logging
from botocore.config import Config
import requests
from botocore.exceptions import ClientError, NoCredentialsError
import time
from twisted.internet import task, reactor
##mysqlimport
#import mysql.connector
from pathlib import Path
from os import path
###AWS INFO####

# print(list_objects_bucket)


class Command(BaseCommand):
    help = 'Linkebis aploadi'

    def handle(self,*args,**kwargs):
        access_key = 'XXXXXXXXXXXXXXXXXXXXXXXX'
        access_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX'
        bucket_name = 'XXXXXXXXXXXXXXXXXXXXXXXX'
        bucket_name2= 'XXXXXXXXXXXXXXXXXXXXXXXX'
        client = boto3.client('s3')
        list_objects_bucket = client.list_objects(Bucket=bucket_name)

        # mydb = mysql.connector.connect(host="localhost", user="newuser",database="cointrack",passwd="password")
        # mycursor = mydb.cursor()
        ####Connet To S3 Service
        client_s3= boto3.client(
            
            's3',
            region_name="eu-west-2",
            aws_access_key_id=access_key,
            aws_secret_access_key=access_secret
        )

        counter = 0
        s3_resource = boto3.resource("s3", region_name="eu-west-2")
        #upload files to S3 Bucker
        data_file_folder = r"//10.0.83.27/Shared/123"
        t1 = time.strftime('%Y-%m-%d %H:%M:%S')
         
        try:
            #bucket_name = "S3_Bucket_Name" #s3 bucket name
            data_file_folder = r"//10.0.83.27/Shared/123/" # local folder for upload

            my_bucket = s3_resource.Bucket(bucket_name)
            my_bucket2= s3_resource.Bucket(bucket_name2)

            for path, subdirs, files in os.walk(data_file_folder):
                path = path.replace("\\","/")
                directory_name = path.replace(data_file_folder,"")
                Destination_dir= "//10.0.83.27/Shared/gadatanilebi/"
                Dest_dir_xelmeored="//10.0.83.277/Shared/Xelmeoredatvirtulebi/"
                for file in files:
                    if os.path.isfile(Destination_dir+file)==False:
                
                        
                        now = datetime.now()
                        my_bucket2.upload_file(os.path.join(path, file),file)
                        t1 = time.strftime('%Y-%m-%d %H:%M:%S')
                        print('Uploading file {0}...'.format(file))
                        print(path)
                        print(t1)
                        
                        counter+=1
                        #shutil.move(path+"/"+file, Destination_dir)
                        print(file)
                        shutil.move((path+"/"+file), os.path.join(Destination_dir,file))
                    else:
                        if os.path.isfile(Destination_dir+file)==True: #### Tu ukve ertxel gadatanili iqneb sxva foldershi gadaitans ro ar gadaawero
                            now = datetime.now()
                            my_bucket.upload_file(os.path.join(path, file),file)#directory_name+'/'+file)  ###bucketze Uploadi
                            my_bucket2.upload_file(os.path.join(path, file),file)
                            t1 = time.strftime('%Y-%m-%d %H:%M:%S')
                            print('Uploading file {0}...'.format(file))
                            print(path)
                            print(t1)
                            
                            #shutil.move(path+"/"+file, Destination_dir)
                            print(file)
                            counter+=1
                            shutil.move((path+"/"+file), os.path.join(Dest_dir_xelmeored,file))
            print(counter)


                        #shutil.copytree(path+"/"+file, Destination_dir, file_exist_ok=True) 

                        

                            # os.rename(file,Destination_dir)


 

当我尝试进入我的本地计算机时正常工作,但是当我进入Windows server 2021 这是错误代码

  File "C:\Users\loc\AppData\Local\Programs\Python\Python39\lib\site-packages\botocore\auth.py", line 357, in add_auth
    raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

什么是问题?凭据是 100% 正确的,因为正如我所说,它在我的本地计算机上工作。 我的意思是,当我在我的计算机上运行时,Windows 10 运行正常,当我运行另一台计算机时,Windows 2012 服务器无法运行

【问题讨论】:

  • 您正在创建两个 s3 客户端对象,一个带有凭据,一个没有。然后你继续使用没有凭据的那个。

标签: python python-3.x amazon-web-services amazon-s3 boto3


【解决方案1】:

尽管您在代码中提供了凭据,但您并没有在任何地方使用它。

它可以在您的本地计算机上运行,​​因为您可能已经安装并配置了 AWS CLI 凭证,并且代码将使用该配置的凭证

以下代码将在代码中使用内联凭据,但建议您使用使用部署到 AWS 的 EC2 实例配置文件设置的凭据或使用 CLI 配置的凭据

import boto3
session = boto3.Session(
    aws_access_key_id=access_key,
    aws_secret_access_key=access_secret
)

 s3_resource = session.resource("s3", region_name="eu-west-2")

【讨论】:

  • 什么意思? “虽然你在代码中提供了凭据,但你并没有在任何地方使用它”,如何修复或正确使用?
  • 查看“session”和“s3_resource”变量的用法并与您的代码进行比较,您将得到答案
猜你喜欢
  • 2016-01-22
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 2019-09-12
  • 2020-02-03
  • 2019-10-27
  • 2019-05-29
  • 1970-01-01
相关资源
最近更新 更多