簡體   English   中英

如何修復 boto3 aws botocore.exceptions.NoCredentialsError:

[英]How to fix boto3 aws botocore.exceptions.NoCredentialsError:

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 服務器不工作

盡管您在代碼中提供了憑據,但您並沒有在任何地方使用它。

它在您的本地計算機上工作,因為您可能安裝了 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM